-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy patherror.rs
More file actions
87 lines (77 loc) · 2.72 KB
/
error.rs
File metadata and controls
87 lines (77 loc) · 2.72 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
use std::fmt;
use payjoin::ImplementationError;
use r2d2::Error as R2d2Error;
use rusqlite::Error as RusqliteError;
pub(crate) type Result<T> = std::result::Result<T, Error>;
#[cfg(feature = "v2")]
#[derive(Debug)]
pub(crate) enum DuplicateKind {
Uri,
ReceiverPubkey,
}
#[derive(Debug)]
pub(crate) enum Error {
Rusqlite(RusqliteError),
R2d2(R2d2Error),
#[cfg(feature = "v2")]
Serialize(serde_json::Error),
#[cfg(feature = "v2")]
Deserialize(serde_json::Error),
#[cfg(feature = "v2")]
DuplicateSendSession(DuplicateKind),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Rusqlite(e) => write!(f, "Database operation failed: {e}"),
Error::R2d2(e) => write!(f, "Connection pool error: {e}"),
#[cfg(feature = "v2")]
Error::Serialize(e) => write!(f, "Serialization failed: {e}"),
#[cfg(feature = "v2")]
Error::Deserialize(e) => write!(f, "Deserialization failed: {e}"),
#[cfg(feature = "v2")]
Error::DuplicateSendSession(DuplicateKind::Uri) => {
write!(f, "A send session for this URI is already active")
}
#[cfg(feature = "v2")]
Error::DuplicateSendSession(DuplicateKind::ReceiverPubkey) => write!(
f,
"A send session with this receiver pubkey is already active under a different URI"
),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Rusqlite(e) => Some(e),
Error::R2d2(e) => Some(e),
#[cfg(feature = "v2")]
Error::Serialize(e) => Some(e),
#[cfg(feature = "v2")]
Error::Deserialize(e) => Some(e),
#[cfg(feature = "v2")]
Error::DuplicateSendSession(_) => None,
}
}
}
impl From<RusqliteError> for Error {
fn from(error: RusqliteError) -> Self { Error::Rusqlite(error) }
}
impl From<R2d2Error> for Error {
fn from(error: R2d2Error) -> Self { Error::R2d2(error) }
}
#[cfg(feature = "v2")]
impl From<serde_json::Error> for Error {
fn from(error: serde_json::Error) -> Self {
match error.classify() {
serde_json::error::Category::Io => Error::Serialize(error), // I/O errors during writing/serialization
serde_json::error::Category::Syntax
| serde_json::error::Category::Data
| serde_json::error::Category::Eof => Error::Deserialize(error), // All parsing/reading errors
}
}
}
impl From<Error> for ImplementationError {
fn from(error: Error) -> Self { ImplementationError::new(error) }
}