nautilus_binance/spot/http/
error.rs1use std::fmt::Display;
19
20use nautilus_network::http::error::HttpClientError;
21
22pub use crate::common::sbe::SbeDecodeError;
24
25#[derive(Debug)]
27pub enum BinanceSpotHttpError {
28 MissingCredentials,
30 BinanceError {
32 code: i64,
34 message: String,
36 },
37 SbeDecodeError(SbeDecodeError),
39 ValidationError(String),
41 NetworkError(String),
43 Timeout(String),
45 Canceled(String),
47 UnexpectedStatus {
49 status: u16,
51 body: String,
53 },
54}
55
56impl Display for BinanceSpotHttpError {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 match self {
59 Self::MissingCredentials => write!(f, "Missing API credentials"),
60 Self::BinanceError { code, message } => {
61 write!(f, "Binance error {code}: {message}")
62 }
63 Self::SbeDecodeError(err) => write!(f, "SBE decode error: {err}"),
64 Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
65 Self::NetworkError(msg) => write!(f, "Network error: {msg}"),
66 Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
67 Self::Canceled(msg) => write!(f, "Canceled: {msg}"),
68 Self::UnexpectedStatus { status, body } => {
69 write!(f, "Unexpected status {status}: {body}")
70 }
71 }
72 }
73}
74
75impl std::error::Error for BinanceSpotHttpError {}
76
77impl From<SbeDecodeError> for BinanceSpotHttpError {
78 fn from(err: SbeDecodeError) -> Self {
79 Self::SbeDecodeError(err)
80 }
81}
82
83impl From<anyhow::Error> for BinanceSpotHttpError {
84 fn from(err: anyhow::Error) -> Self {
85 Self::NetworkError(err.to_string())
86 }
87}
88
89impl From<HttpClientError> for BinanceSpotHttpError {
90 fn from(err: HttpClientError) -> Self {
91 match err {
92 HttpClientError::TimeoutError(msg) => Self::Timeout(msg),
93 HttpClientError::InvalidProxy(msg) | HttpClientError::ClientBuildError(msg) => {
94 Self::NetworkError(msg)
95 }
96 HttpClientError::Error(msg) => Self::NetworkError(msg),
97 }
98 }
99}
100
101pub type BinanceSpotHttpResult<T> = Result<T, BinanceSpotHttpError>;