nautilus_binance/http/
error.rs1use std::fmt::{self, Display};
19
20use nautilus_network::http::error::HttpClientError;
21
22#[derive(Debug)]
24pub enum BinanceHttpError {
25 MissingCredentials,
27 BinanceError {
29 code: i64,
31 message: String,
33 },
34 JsonError(String),
36 ValidationError(String),
38 NetworkError(String),
40 Timeout(String),
42 Canceled(String),
44 UnexpectedStatus {
46 status: u16,
48 body: String,
50 },
51}
52
53impl Display for BinanceHttpError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 Self::MissingCredentials => write!(f, "Missing API credentials"),
57 Self::BinanceError { code, message } => {
58 write!(f, "Binance error {code}: {message}")
59 }
60 Self::JsonError(msg) => write!(f, "JSON error: {msg}"),
61 Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
62 Self::NetworkError(msg) => write!(f, "Network error: {msg}"),
63 Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
64 Self::Canceled(msg) => write!(f, "Canceled: {msg}"),
65 Self::UnexpectedStatus { status, body } => {
66 write!(f, "Unexpected status {status}: {body}")
67 }
68 }
69 }
70}
71
72impl std::error::Error for BinanceHttpError {}
73
74impl From<serde_json::Error> for BinanceHttpError {
75 fn from(err: serde_json::Error) -> Self {
76 Self::JsonError(err.to_string())
77 }
78}
79
80impl From<anyhow::Error> for BinanceHttpError {
81 fn from(err: anyhow::Error) -> Self {
82 Self::NetworkError(err.to_string())
83 }
84}
85
86impl From<HttpClientError> for BinanceHttpError {
87 fn from(err: HttpClientError) -> Self {
88 match err {
89 HttpClientError::TimeoutError(msg) => Self::Timeout(msg),
90 HttpClientError::InvalidProxy(msg) | HttpClientError::ClientBuildError(msg) => {
91 Self::NetworkError(msg)
92 }
93 HttpClientError::Error(msg) => Self::NetworkError(msg),
94 }
95 }
96}
97
98pub type BinanceHttpResult<T> = Result<T, BinanceHttpError>;