Skip to main content

bybit/
errors.rs

1//! This module contains the definitions for BybitContentError and BybitError, two custom error types.
2//! BybitContentError is a struct that represents the error returned by the Bybit API, and BybitError is
3//! an enum that can hold any possible error that can occur during the execution of the program.
4
5use crate::prelude::*;
6
7/// BybitContentError is a struct that represents the error returned by the Bybit API.
8/// It has two fields: code, which is an i16 representing the error code, and msg, which is a String
9/// representing the error message.
10#[derive(Debug, Deserialize, Display)]
11#[display("{}", msg)]
12pub struct BybitContentError {
13    /// The raw error code returned by the Bybit API, you can use this to map
14    /// to a specific error type by calling `self.typed()`, this is useful if
15    /// you want to match on a specific error type.
16    pub code: i32,
17
18    /// The error message returned by the Bybit API.
19    pub msg: String,
20}
21
22impl BybitContentError {
23    /// A typed version of the error code, this is useful if you want to match
24    /// on a specific error type.
25    pub fn typed(&self) -> Option<ReturnCode> {
26        ReturnCode::from_code(self.code)
27    }
28}
29
30/// BybitError is an enum that can hold any possible error that can occur during the execution of the program.
31/// It has several variants, each representing a different type of error.
32#[derive(Debug, Error)]
33pub enum BybitError {
34    /// BybitError variant that holds a BybitContentError. This is used when the error returned by the Bybit API
35    /// is of the type BybitContentError.
36    #[error("Bybit error: {0}")]
37    BybitError(BybitContentError),
38
39    #[error("Failed to emit value on channel, underlying: {underlying}")]
40    ChannelSendError { underlying: String },
41
42    /// ValidationError variant that holds a validation error message.
43    /// This variant is used when request parameters fail validation before being sent to the API.
44    #[error("Validation error: {0}")]
45    ValidationError(String),
46
47    /// KlineValueMissingError variant that holds the index of the missing value, and the name of the missing value.
48    /// This variant is used when a value in a kline vector is missing.
49    #[error("Invalid Vec for Kline: {name} at {index} is missing")]
50    KlineValueMissingError { index: usize, name: &'static str },
51
52    /// Variants that hold the error returned by reqwest, serde_json, tokio_tungstenite, and std libraries.
53    /// These variants are used when the respective library returns an error.
54    #[error(transparent)]
55    ReqError(#[from] reqwest::Error),
56
57    #[error(transparent)]
58    InvalidHeaderError(#[from] reqwest::header::InvalidHeaderValue),
59
60    #[error(transparent)]
61    IoError(#[from] std::io::Error),
62
63    #[error(transparent)]
64    ParseFloatError(#[from] std::num::ParseFloatError),
65
66    #[error(transparent)]
67    UrlParserError(#[from] url::ParseError),
68
69    #[error(transparent)]
70    Json(#[from] serde_json::Error),
71
72    #[error(transparent)]
73    Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
74
75    #[error(transparent)]
76    TimestampError(#[from] std::time::SystemTimeError),
77
78    #[error(transparent)]
79    SerdeError(#[from] serde::de::value::Error),
80
81    // Variants representing common errors.
82    #[error("Internal Server Error")]
83    InternalServerError,
84
85    #[error("Service Unavailable")]
86    ServiceUnavailable,
87
88    #[error("Unauthorized")]
89    Unauthorized,
90
91    /// StatusCode variant that holds the status code.
92    #[error("Status Code")]
93    StatusCode(u16),
94
95    /// Base variant that holds a String representing the error.
96    /// This variant is used when the error is not of any specific type, and it is just a simple String.
97    #[error("Bybit error: {0}")]
98    Base(String),
99}
100
101// Implement the From trait for String and BybitError.
102// This trait is used to specify how a String can be converted to BybitError.
103impl From<std::string::String> for BybitError {
104    // This function takes a String, and returns a BybitError.
105    fn from(err: String) -> Self {
106        //
107        // Convert the String error to BybitError here
108        // For example, you can return a new instance of BybitError with the error message
109        BybitError::new(err)
110    }
111}
112
113impl BybitError {
114    fn new(arg: String) -> Self {
115        BybitError::Base(arg)
116    }
117}