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)]
46pub struct ApiError {
47 pub code: ErrorCode,
48 pub msg: String,
49}
50
51impl From<ApiError> for Error {
52 fn from(err: ApiError) -> Self {
53 Error::Api(err)
54 }
55}
56
57impl From<std::io::Error> for Error {
58 fn from(err: std::io::Error) -> Self {
59 Error::Io(err)
60 }
61}
62
63impl From<String> for Error {
64 fn from(msg: String) -> Self {
65 Error::Msg(msg)
66 }
67}
68
69impl From<&str> for Error {
70 fn from(msg: &str) -> Self {
71 Error::Msg(msg.to_string())
72 }
73}
74
75impl From<reqwest::Error> for Error {
76 fn from(err: reqwest::Error) -> Self {
77 Error::Reqwest(err)
78 }
79}
80
81impl From<serde_json::Error> for Error {
82 fn from(err: serde_json::Error) -> Self {
83 Error::SerdeJson(err)
84 }
85}
86
87impl From<serde_urlencoded::ser::Error> for Error {
88 fn from(err: serde_urlencoded::ser::Error) -> Self {
89 Error::SerdeUrlEncoded(err)
90 }
91}
92
93impl From<serde_path_to_error::Error<serde_json::Error>> for Error {
94 fn from(err: serde_path_to_error::Error<serde_json::Error>) -> Self {
95 Error::SerdePathToError(err)
96 }
97}