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    /// KlineValueMissingError variant that holds the index of the missing value, and the name of the missing value.
43    /// This variant is used when a value in a kline vector is missing.
44    #[error("Invalid Vec for Kline: {name} at {index} is missing")]
45    KlineValueMissingError { index: usize, name: &'static str },
46
47    /// Variants that hold the error returned by reqwest, serde_json, tokio_tungstenite, and std libraries.
48    /// These variants are used when the respective library returns an error.
49    #[error(transparent)]
50    ReqError(#[from] reqwest::Error),
51
52    #[error(transparent)]
53    InvalidHeaderError(#[from] reqwest::header::InvalidHeaderValue),
54
55    #[error(transparent)]
56    IoError(#[from] std::io::Error),
57
58    #[error(transparent)]
59    ParseFloatError(#[from] std::num::ParseFloatError),
60
61    #[error(transparent)]
62    UrlParserError(#[from] url::ParseError),
63
64    #[error(transparent)]
65    Json(#[from] serde_json::Error),
66
67    #[error(transparent)]
68    Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
69
70    #[error(transparent)]
71    TimestampError(#[from] std::time::SystemTimeError),
72
73    #[error(transparent)]
74    SerdeError(#[from] serde::de::value::Error),
75
76    // Variants representing common errors.
77    #[error("Internal Server Error")]
78    InternalServerError,
79
80    #[error("Service Unavailable")]
81    ServiceUnavailable,
82
83    #[error("Unauthorized")]
84    Unauthorized,
85
86    /// StatusCode variant that holds the status code.
87    #[error("Status Code")]
88    StatusCode(u16),
89
90    /// Base variant that holds a String representing the error.
91    /// This variant is used when the error is not of any specific type, and it is just a simple String.
92    #[error("Bybit error: {0}")]
93    Base(String),
94}
95
96// Implement the From trait for String and BybitError.
97// This trait is used to specify how a String can be converted to BybitError.
98impl From<std::string::String> for BybitError {
99    // This function takes a String, and returns a BybitError.
100    fn from(err: String) -> Self {
101        //
102        // Convert the String error to BybitError here
103        // For example, you can return a new instance of BybitError with the error message
104        BybitError::new(err)
105    }
106}
107
108impl BybitError {
109    fn new(arg: String) -> Self {
110        BybitError::Base(arg)
111    }
112}