use thiserror::Error;
#[derive(Debug, Error)]
pub enum BybitError {
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
#[error("WebSocket error: {0}")]
WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
#[error("Request timeout")]
Timeout,
#[error("API error: code={code}, msg={msg}")]
Api {
code: i32,
msg: String,
},
#[error("Parse error: {0}")]
Parse(String),
#[error("Missing field: {0}")]
MissingField(&'static str),
#[error("Invalid parameter: {0}")]
InvalidParam(String),
#[error("Authentication error: {0}")]
Auth(String),
}
pub type Result<T> = std::result::Result<T, BybitError>;
impl BybitError {
pub fn api(code: i32, msg: impl Into<String>) -> Self {
Self::Api {
code,
msg: msg.into(),
}
}
pub fn parse(msg: impl Into<String>) -> Self {
Self::Parse(msg.into())
}
pub fn invalid_param(msg: impl Into<String>) -> Self {
Self::InvalidParam(msg.into())
}
pub fn is_rate_limited(&self) -> bool {
matches!(self, Self::Api { code: 10006, .. })
}
pub fn is_timeout(&self) -> bool {
matches!(self, Self::Timeout)
}
}