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
pub(crate) use crate::ffi::ErrorCode;
use std::fmt::Display;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error {
ApiNotAvailable,
Internal,
HttpRequestFailed,
InvalidArguments,
NotFound,
Unavailable,
Other(u32),
}
#[allow(clippy::fallible_impl_from)] impl From<ErrorCode> for Error {
fn from(code: ErrorCode) -> Self {
match code {
ErrorCode::Success => panic!("Unexpected ErrorCode::Success"),
ErrorCode::ApiNotAvailable => Self::ApiNotAvailable,
ErrorCode::InternalError => Self::Internal,
ErrorCode::HttpRequestFailed => Self::HttpRequestFailed,
ErrorCode::InvalidArguments => Self::InvalidArguments,
ErrorCode::NotFound => Self::NotFound,
ErrorCode::Unavailable => Self::Unavailable,
code => Self::Other(code as u32),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ApiNotAvailable => write!(f, "API not available"),
Self::Internal => write!(f, "Internal error"),
Self::HttpRequestFailed => {
write!(f, "HTTP request failed due to external server error")
}
Self::InvalidArguments => write!(f, "Invalid arguments"),
Self::NotFound => write!(f, "Not found"),
Self::Unavailable => write!(f, "Unavailable"),
Self::Other(code) => write!(f, "Unknown code {code}"),
}?;
Ok(())
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}