Skip to main content

binance/
errors.rs

1use serde::Deserialize;
2use thiserror::Error;
3
4pub type Result<T> = std::result::Result<T, SdkError>;
5
6#[derive(Error, Debug)]
7pub enum SdkError {
8    #[error("{0}")]
9    BinanceError(#[from] BinanceContentError),
10
11    #[error("{name} at {index} is missing")]
12    KlineValueMissingError { name: String, index: usize },
13
14    // Box the large error types
15    #[error("Request error")]
16    ReqError(#[from] Box<reqwest::Error>),
17
18    #[error("Tungstenite error")]
19    Tungstenite(#[from] Box<tungstenite::Error>),
20
21    // Keep small errors unboxed
22    #[error("Invalid header error")]
23    InvalidHeaderError(#[from] reqwest::header::InvalidHeaderValue),
24
25    #[error("IO error")]
26    IoError(#[from] std::io::Error),
27
28    #[error("Parse float error")]
29    ParseFloatError(#[from] std::num::ParseFloatError),
30
31    #[error("URL parser error")]
32    UrlParserError(#[from] url::ParseError),
33
34    #[error("JSON error")]
35    Json(#[from] serde_json::Error),
36
37    #[error("Timestamp error")]
38    TimestampError(#[from] std::time::SystemTimeError),
39
40    #[error("{0}")]
41    Other(String),
42}
43
44impl From<reqwest::Error> for SdkError {
45    fn from(err: reqwest::Error) -> Self {
46        SdkError::ReqError(Box::new(err))
47    }
48}
49
50impl From<tungstenite::Error> for SdkError {
51    fn from(err: tungstenite::Error) -> Self {
52        SdkError::Tungstenite(Box::new(err))
53    }
54}
55
56impl From<String> for SdkError {
57    fn from(err: String) -> Self {
58        SdkError::Other(err)
59    }
60}
61
62#[derive(Error, Debug, Clone, Deserialize)]
63#[error("Binance content error: {msg} (code: {code})")]
64pub struct BinanceContentError {
65    pub code: i16,
66    pub msg: String,
67}