1#[derive(Debug)]
2pub enum Error {
3 Api { code: i64, msg: String },
4 InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
5 Io(std::io::Error),
6 Msg(String),
7 Reqwest(reqwest::Error),
8 SerdeJson(serde_json::Error),
9 SerdeUrlEncoded(serde_urlencoded::ser::Error),
10 SerdePathToError(serde_path_to_error::Error<serde_json::Error>),
11}
12
13impl std::fmt::Display for Error {
14 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 match self {
16 Error::Api { code, msg } => write!(f, "Bybit API error: code: {code}, message: {msg}"),
17 Error::InvalidHeaderValue(error) => write!(f, "invalid header value: {error}"),
18 Error::Io(error) => write!(f, "I/O error: {error}"),
19 Error::Msg(msg) => write!(f, "{msg}"),
20 Error::Reqwest(error) => write!(f, "reqwest error: {error}"),
21 Error::SerdeJson(error) => write!(f, "serde_json error: {error}"),
22 Error::SerdeUrlEncoded(error) => write!(f, "serde_urlencoded error: {error}"),
23 Error::SerdePathToError(error) => write!(
24 f,
25 "serde_path_to_error error: path: {}, msg: {}",
26 error.path(),
27 error.inner()
28 ),
29 }
30 }
31}
32
33impl std::error::Error for Error {}
34
35impl From<std::io::Error> for Error {
36 fn from(err: std::io::Error) -> Self {
37 Error::Io(err)
38 }
39}
40
41impl From<String> for Error {
42 fn from(msg: String) -> Self {
43 Error::Msg(msg)
44 }
45}
46
47impl From<&str> for Error {
48 fn from(msg: &str) -> Self {
49 Error::Msg(msg.to_string())
50 }
51}
52
53impl From<super::http::APIErrorResponse> for Error {
54 fn from(resp: super::http::APIErrorResponse) -> Self {
55 Self::Api {
56 code: resp.ret_code,
57 msg: resp.ret_msg,
58 }
59 }
60}
61
62impl From<reqwest::header::InvalidHeaderValue> for Error {
63 fn from(err: reqwest::header::InvalidHeaderValue) -> Self {
64 Error::InvalidHeaderValue(err)
65 }
66}
67
68impl From<reqwest::Error> for Error {
69 fn from(err: reqwest::Error) -> Self {
70 Error::Reqwest(err)
71 }
72}
73
74impl From<serde_json::Error> for Error {
75 fn from(err: serde_json::Error) -> Self {
76 Error::SerdeJson(err)
77 }
78}
79
80impl From<serde_urlencoded::ser::Error> for Error {
81 fn from(err: serde_urlencoded::ser::Error) -> Self {
82 Error::SerdeUrlEncoded(err)
83 }
84}
85
86impl From<serde_path_to_error::Error<serde_json::Error>> for Error {
87 fn from(err: serde_path_to_error::Error<serde_json::Error>) -> Self {
88 Error::SerdePathToError(err)
89 }
90}