nautilus_binance/papi/http/
error.rs1use std::{fmt::Display, time::Duration};
19
20use nautilus_network::http::error::HttpClientError;
21
22use crate::common::error::{
23 BINANCE_IP_BAN_STATUS, BINANCE_RATE_LIMIT_BLOCKED_MESSAGE, is_retryable_http_status,
24 is_retryable_venue_code,
25};
26
27#[derive(Debug)]
29pub enum BinancePapiHttpError {
30 MissingCredentials,
32 UnsupportedSignatureScheme(String),
34 BinanceError {
36 code: i64,
38 message: String,
40 status: u16,
42 retry_after: Option<Duration>,
44 },
45 JsonError(String),
47 ValidationError(String),
49 NetworkError(String),
51 Timeout(String),
53 Canceled(String),
55 RateLimitBlocked,
57 RetryBudgetExceeded(String),
59 UnexpectedStatus {
61 status: u16,
63 body: String,
65 retry_after: Option<Duration>,
67 },
68}
69
70impl BinancePapiHttpError {
71 #[must_use]
76 pub fn is_retryable(&self) -> bool {
77 match self {
78 Self::BinanceError {
79 status: BINANCE_IP_BAN_STATUS,
80 retry_after: None,
81 ..
82 }
83 | Self::UnexpectedStatus {
84 status: BINANCE_IP_BAN_STATUS,
85 retry_after: None,
86 ..
87 } => false,
88 Self::NetworkError(_) | Self::Timeout(_) => true,
89 Self::BinanceError { code, status, .. } => {
90 is_retryable_venue_code(*code) || is_retryable_http_status(*status)
91 }
92 Self::UnexpectedStatus { status, .. } => is_retryable_http_status(*status),
93 _ => false,
94 }
95 }
96
97 #[must_use]
99 pub fn retry_after(&self) -> Option<Duration> {
100 match self {
101 Self::BinanceError { retry_after, .. } | Self::UnexpectedStatus { retry_after, .. } => {
102 *retry_after
103 }
104 _ => None,
105 }
106 }
107}
108
109impl Display for BinancePapiHttpError {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 match self {
112 Self::MissingCredentials => f.write_str("Missing API credentials"),
113 Self::UnsupportedSignatureScheme(scheme) => {
114 write!(f, "Unsupported PAPI signature scheme: {scheme}")
115 }
116 Self::BinanceError {
117 code,
118 message,
119 status,
120 ..
121 } => {
122 write!(f, "Binance error {code} (HTTP {status}): {message}")
123 }
124 Self::JsonError(msg) => write!(f, "JSON error: {msg}"),
125 Self::ValidationError(msg) => write!(f, "Validation error: {msg}"),
126 Self::NetworkError(msg) => write!(f, "Network error: {msg}"),
127 Self::Timeout(msg) => write!(f, "Timeout: {msg}"),
128 Self::Canceled(msg) => write!(f, "Canceled: {msg}"),
129 Self::RateLimitBlocked => f.write_str(BINANCE_RATE_LIMIT_BLOCKED_MESSAGE),
130 Self::RetryBudgetExceeded(msg) => write!(f, "Retry budget exceeded: {msg}"),
131 Self::UnexpectedStatus { status, body, .. } => {
132 write!(f, "Unexpected status {status}: {body}")
133 }
134 }
135 }
136}
137
138impl std::error::Error for BinancePapiHttpError {}
139
140impl From<serde_json::Error> for BinancePapiHttpError {
141 fn from(err: serde_json::Error) -> Self {
142 Self::JsonError(err.to_string())
143 }
144}
145
146impl From<anyhow::Error> for BinancePapiHttpError {
147 fn from(err: anyhow::Error) -> Self {
148 Self::NetworkError(err.to_string())
149 }
150}
151
152impl From<HttpClientError> for BinancePapiHttpError {
153 fn from(err: HttpClientError) -> Self {
154 match err {
155 HttpClientError::TimeoutError(msg) => Self::Timeout(msg),
156 HttpClientError::InvalidProxy(msg) | HttpClientError::ClientBuildError(msg) => {
157 Self::NetworkError(msg)
158 }
159 HttpClientError::Error(msg) | HttpClientError::TransportError(msg) => {
160 Self::NetworkError(msg)
161 }
162 }
163 }
164}
165
166pub type BinancePapiHttpResult<T> = Result<T, BinancePapiHttpError>;