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