1use serde::Deserialize;
2
3pub use crate::ErrorCode;
6
7#[derive(Debug)]
8pub enum Error {
9 Api(ApiError),
10 Io(std::io::Error),
11 Msg(String),
12 Reqwest(reqwest::Error),
13 SerdeJson(serde_json::Error),
14 SerdeUrlEncoded(serde_urlencoded::ser::Error),
15 SerdePathToError(serde_path_to_error::Error<serde_json::Error>),
16}
17
18impl std::fmt::Display for Error {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 match self {
21 Error::Api(error) => write!(f, "API error: code: {}, msg: {}", error.code, error.msg),
22 Error::Io(error) => write!(f, "I/O error: {error}"),
23 Error::Msg(msg) => write!(f, "{msg}"),
24 Error::Reqwest(error) => write!(f, "reqwest error: {error}"),
25 Error::SerdeJson(error) => write!(f, "serde_json error: {error}"),
26 Error::SerdeUrlEncoded(error) => write!(f, "serde_urlencoded error: {error}"),
27 Error::SerdePathToError(error) => write!(
28 f,
29 "serde_path_to_error error: path: {}, msg: {}",
30 error.path(),
31 error.inner()
32 ),
33 }
34 }
35}
36
37impl std::error::Error for Error {}
38
39#[derive(Debug, Deserialize, PartialEq)]
47pub struct ApiError {
48 pub code: ErrorCode,
49 pub msg: String,
50}
51
52impl From<ApiError> for Error {
53 fn from(err: ApiError) -> Self {
54 Error::Api(err)
55 }
56}
57
58impl From<std::io::Error> for Error {
59 fn from(err: std::io::Error) -> Self {
60 Error::Io(err)
61 }
62}
63
64impl From<String> for Error {
65 fn from(msg: String) -> Self {
66 Error::Msg(msg)
67 }
68}
69
70impl From<&str> for Error {
71 fn from(msg: &str) -> Self {
72 Error::Msg(msg.to_string())
73 }
74}
75
76impl From<reqwest::Error> for Error {
77 fn from(err: reqwest::Error) -> Self {
78 Error::Reqwest(err)
79 }
80}
81
82impl From<serde_json::Error> for Error {
83 fn from(err: serde_json::Error) -> Self {
84 Error::SerdeJson(err)
85 }
86}
87
88impl From<serde_urlencoded::ser::Error> for Error {
89 fn from(err: serde_urlencoded::ser::Error) -> Self {
90 Error::SerdeUrlEncoded(err)
91 }
92}
93
94impl From<serde_path_to_error::Error<serde_json::Error>> for Error {
95 fn from(err: serde_path_to_error::Error<serde_json::Error>) -> Self {
96 Error::SerdePathToError(err)
97 }
98}