-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy patherror.rs
More file actions
113 lines (106 loc) · 3.02 KB
/
error.rs
File metadata and controls
113 lines (106 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use compact_encoding::EncodingError;
use random_access_storage::RandomAccessError;
use thiserror::Error;
use crate::Store;
/// Common error type for the hypercore interface
#[derive(Error, Debug)]
pub enum HypercoreError {
/// Bad argument
#[error("Bad argument. {context}")]
BadArgument {
/// Context for the error
context: String,
},
/// Not writable
#[error("Hypercore not writable")]
NotWritable,
/// Invalid signature
#[error("Given signature was invalid. {context}")]
InvalidSignature {
/// Context for the error
context: String,
},
/// Invalid checksum
#[error("Invalid checksum. {context}")]
InvalidChecksum {
/// Context for the error
context: String,
},
/// Empty storage
#[error("Empty storage: {store}.")]
EmptyStorage {
/// Store that was found empty
store: Store,
},
/// Corrupt storage
#[error("Corrupt storage: {store}.{}",
.context.as_ref().map_or_else(String::new, |ctx| format!(" Context: {ctx}.")))]
CorruptStorage {
/// Store that was corrupt
store: Store,
/// Context for the error
context: Option<String>,
},
/// Invalid operation
#[error("Invalid operation. {context}")]
InvalidOperation {
/// Context for the error
context: String,
},
/// Unexpected IO error occured
#[error("Unrecoverable input/output error occured.{}",
.context.as_ref().map_or_else(String::new, |ctx| format!(" {ctx}.")))]
IO {
/// Context for the error
context: Option<String>,
/// Original source error
#[source]
source: std::io::Error,
},
#[cfg(feature = "replication")]
#[error("hypercore_protocol Error")]
Protocol(#[from] hypercore_protocol::Error),
}
impl From<std::io::Error> for HypercoreError {
fn from(err: std::io::Error) -> Self {
Self::IO {
context: None,
source: err,
}
}
}
impl From<EncodingError> for HypercoreError {
fn from(err: EncodingError) -> Self {
Self::InvalidOperation {
context: format!("Encoding failed: {err}"),
}
}
}
impl From<RandomAccessError> for HypercoreError {
fn from(value: RandomAccessError) -> Self {
map_random_access_err(value)
}
}
pub(crate) fn map_random_access_err(err: RandomAccessError) -> HypercoreError {
match err {
RandomAccessError::IO {
return_code,
context,
source,
} => HypercoreError::IO {
context: Some(format!(
"RandomAccess IO error. Context: {context:?}, return_code: {return_code:?}",
)),
source,
},
RandomAccessError::OutOfBounds {
offset,
end,
length,
} => HypercoreError::InvalidOperation {
context: format!(
"RandomAccess out of bounds. Offset: {offset}, end: {end:?}, length: {length}",
),
},
}
}