nautilus_binance/spot/http/
error.rs1use std::fmt::{self, Display};
19
20use nautilus_network::http::error::HttpClientError;
21
22#[derive(Debug)]
24pub enum BinanceSpotHttpError {
25 MissingCredentials,
27 BinanceError {
29 code: i64,
31 message: String,
33 },
34 SbeDecodeError(SbeDecodeError),
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 BinanceSpotHttpError {
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::SbeDecodeError(err) => write!(f, "SBE decode error: {err}"),
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 BinanceSpotHttpError {}
73
74impl From<SbeDecodeError> for BinanceSpotHttpError {
75 fn from(err: SbeDecodeError) -> Self {
76 Self::SbeDecodeError(err)
77 }
78}
79
80impl From<anyhow::Error> for BinanceSpotHttpError {
81 fn from(err: anyhow::Error) -> Self {
82 Self::NetworkError(err.to_string())
83 }
84}
85
86impl From<HttpClientError> for BinanceSpotHttpError {
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 BinanceSpotHttpResult<T> = Result<T, BinanceSpotHttpError>;
100
101#[derive(Debug, Clone, PartialEq, Eq)]
103pub enum SbeDecodeError {
104 BufferTooShort { expected: usize, actual: usize },
106 SchemaMismatch { expected: u16, actual: u16 },
108 VersionMismatch { expected: u16, actual: u16 },
110 UnknownTemplateId(u16),
112 GroupSizeTooLarge { count: u32, max: u32 },
114 InvalidUtf8,
116}
117
118impl Display for SbeDecodeError {
119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 match self {
121 Self::BufferTooShort { expected, actual } => {
122 write!(
123 f,
124 "Buffer too short: expected {expected} bytes, got {actual}"
125 )
126 }
127 Self::SchemaMismatch { expected, actual } => {
128 write!(f, "Schema ID mismatch: expected {expected}, got {actual}")
129 }
130 Self::VersionMismatch { expected, actual } => {
131 write!(
132 f,
133 "Schema version mismatch: expected {expected}, got {actual}"
134 )
135 }
136 Self::UnknownTemplateId(id) => write!(f, "Unknown template ID: {id}"),
137 Self::GroupSizeTooLarge { count, max } => {
138 write!(f, "Group size {count} exceeds maximum {max}")
139 }
140 Self::InvalidUtf8 => write!(f, "Invalid UTF-8 in string field"),
141 }
142 }
143}
144
145impl std::error::Error for SbeDecodeError {}