use exc_core::ExchangeError;
use thiserror::Error;
use crate::{
key::SignError,
websocket::types::{
messages::{request::WsRequest, Args},
response::StatusKind,
},
};
#[derive(Debug, Error)]
pub enum OkxError {
#[error("stream is dropped")]
StreamDropped,
#[error("weboscket: {0}")]
Websocket(#[from] tokio_tungstenite::tungstenite::Error),
#[error("remote closed")]
RemoteClosed,
#[error("connection error: {0}")]
Connection(Box<dyn std::error::Error + Send + Sync>),
#[error("websocket disconnected")]
WebsocketDisconnected,
#[error("ping error: {0}")]
Ping(anyhow::Error),
#[error("ping timeout")]
PingTimeout,
#[error("json: {0}")]
Json(#[from] serde_json::Error),
#[error("request sender dropped")]
RequestSenderDropped,
#[error("dispatch error: req={0:?}")]
Dispatch(WsRequest),
#[error("responser error: {0}")]
Callback(#[from] tokio::sync::oneshot::error::RecvError),
#[error("already subscribed or unsubscribping {0:?}")]
SubscribedOrUnsubscribing(Args),
#[error("subscribing or unsubscribing {0:?}")]
SubscribingOrUnsubscribing(Args),
#[error("websocket closed")]
WebsocketClosed,
#[error("api error: {0}")]
Api(StatusKind),
#[error("protocol: {0}")]
Protocol(anyhow::Error),
#[error(transparent)]
Layer(Box<dyn std::error::Error + Send + Sync>),
#[error(transparent)]
Buffer(Box<dyn std::error::Error + Send + Sync>),
#[error("unpexpected data type: {0}")]
UnexpectedDataType(anyhow::Error),
#[error("okx key sign error: {0}")]
SignError(#[from] SignError),
#[error("login error: {0}")]
LoginError(anyhow::Error),
#[error("parsing order: {0}")]
ParsingOrder(String),
#[cfg(feature = "prefer-client-id")]
#[error("missing client id (feature `prefer-client-id` is enabled)")]
MissingClientId,
#[error("parse symbol error: {0}")]
ParseSymbol(#[from] exc_core::ParseSymbolError),
#[error("failed to build exc symbol")]
FailedToBuildExcSymbol,
}
impl OkxError {
pub fn parsing_order(msg: impl ToString) -> Self {
Self::ParsingOrder(msg.to_string())
}
}
impl From<OkxError> for ExchangeError {
fn from(err: OkxError) -> Self {
Self::Other(err.into())
}
}