1use thiserror::Error;
4
5#[derive(Debug, Error)]
9pub enum BybitError {
10 #[error("HTTP error: {0}")]
12 Http(#[from] reqwest::Error),
13
14 #[error("WebSocket error: {0}")]
16 WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
17
18 #[error("Request timeout")]
20 Timeout,
21
22 #[error("API error: code={code}, msg={msg}")]
24 Api {
25 code: i32,
27 msg: String,
29 },
30
31 #[error("Parse error: {0}")]
33 Parse(String),
34
35 #[error("Missing field: {0}")]
37 MissingField(&'static str),
38
39 #[error("Invalid parameter: {0}")]
41 InvalidParam(String),
42
43 #[error("Authentication error: {0}")]
45 Auth(String),
46}
47
48pub type Result<T> = std::result::Result<T, BybitError>;
50
51impl BybitError {
52 pub fn api(code: i32, msg: impl Into<String>) -> Self {
54 Self::Api {
55 code,
56 msg: msg.into(),
57 }
58 }
59
60 pub fn parse(msg: impl Into<String>) -> Self {
62 Self::Parse(msg.into())
63 }
64
65 pub fn invalid_param(msg: impl Into<String>) -> Self {
67 Self::InvalidParam(msg.into())
68 }
69
70 pub fn is_rate_limited(&self) -> bool {
72 matches!(self, Self::Api { code: 10006, .. })
73 }
74
75 pub fn is_timeout(&self) -> bool {
77 matches!(self, Self::Timeout)
78 }
79}