binance/
errors.rs

1use serde_json::Value;
2use std::collections::HashMap;
3use thiserror::Error;
4
5#[derive(Debug, Deserialize, Error)]
6#[error("code: {code}, msg: {msg}")]
7pub struct BinanceContentError {
8    pub code: i32,
9    pub msg: String,
10
11    #[serde(flatten)]
12    extra: HashMap<String, Value>,
13}
14
15/// First errors are technical errors
16/// All unhandled binance content errors are BinanceError
17/// The rest are binance content errors that are properly handled
18/// Unhandled binance errors are Msg
19#[derive(Error, Debug)]
20pub enum Error {
21    #[error(transparent)]
22    ReqError(#[from] reqwest::Error),
23    #[error(transparent)]
24    InvalidHeaderError(#[from] reqwest::header::InvalidHeaderValue),
25    #[error(transparent)]
26    IoError(#[from] std::io::Error),
27    #[error(transparent)]
28    ParseFloatError(#[from] std::num::ParseFloatError),
29    #[error(transparent)]
30    UrlParserError(#[from] url::ParseError),
31    #[error(transparent)]
32    Json(#[from] serde_json::Error),
33    #[error(transparent)]
34    Qs(#[from] serde_qs::Error),
35    #[error(transparent)]
36    Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
37    #[error(transparent)]
38    TimestampError(#[from] std::time::SystemTimeError),
39    #[error(transparent)]
40    UTF8Err(#[from] std::str::Utf8Error),
41    #[error("{response}")]
42    BinanceError {
43        #[from]
44        response: BinanceContentError,
45    },
46    #[error("invalid listen key : {0}")]
47    InvalidListenKey(String),
48    #[error("unknown symbol {0}")]
49    UnknownSymbol(String),
50    #[error("{msg}")]
51    InvalidOrderError { msg: String },
52    #[error("invalid price")]
53    InvalidPrice,
54    #[error("invalid period {0}")]
55    InvalidPeriod(String),
56    #[error("internal server error")]
57    InternalServerError,
58    #[error("service unavailable")]
59    ServiceUnavailable,
60    #[error("Unauthorized")]
61    Unauthorized,
62    #[error("{0}")]
63    Msg(String),
64}
65
66/// Custom error messages
67pub mod error_messages {
68    pub const INVALID_PRICE: &str = "Invalid price.";
69}
70
71pub type Result<T> = core::result::Result<T, Error>;