use nautilus_network::error::SendError;
use thiserror::Error;
pub type BybitWsResult<T> = Result<T, BybitWsError>;
#[derive(Clone, Debug, Error)]
pub enum BybitWsError {
#[error("WebSocket not connected")]
NotConnected,
#[error("WebSocket send error: {0}")]
Send(String),
#[error("WebSocket transport error: {0}")]
Transport(String),
#[error("JSON error: {0}")]
Json(String),
#[error("Authentication error: {0}")]
Authentication(String),
#[error("Client error: {0}")]
ClientError(String),
}
impl From<SendError> for BybitWsError {
fn from(error: SendError) -> Self {
Self::Send(error.to_string())
}
}
impl From<tokio_tungstenite::tungstenite::Error> for BybitWsError {
fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
Self::Transport(error.to_string())
}
}
impl From<serde_json::Error> for BybitWsError {
fn from(error: serde_json::Error) -> Self {
Self::Json(error.to_string())
}
}
impl From<String> for BybitWsError {
fn from(msg: String) -> Self {
Self::Authentication(msg)
}
}
pub(crate) fn should_retry_bybit_error(error: &BybitWsError) -> bool {
match error {
BybitWsError::Transport(_) => true,
BybitWsError::Send(_) => true,
BybitWsError::ClientError(msg) => {
let msg_lower = msg.to_lowercase();
msg_lower.contains("timeout")
|| msg_lower.contains("timed out")
|| msg_lower.contains("connection")
|| msg_lower.contains("network")
}
BybitWsError::NotConnected => true,
BybitWsError::Authentication(_) | BybitWsError::Json(_) => false,
}
}
pub(crate) fn create_bybit_timeout_error(msg: String) -> BybitWsError {
BybitWsError::ClientError(msg)
}