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.
4use serde::Deserialize;
5use std::fmt;
6use thiserror::Error;
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)]
11pub struct BybitContentError {
12 pub code: i16,
13 pub msg: String,
14}
15
16/// BybitError is an enum that can hold any possible error that can occur during the execution of the program.
17/// It has several variants, each representing a different type of error.
18#[derive(Debug, Error)]
19pub enum BybitError {
20 /// BybitError variant that holds a BybitContentError. This is used when the error returned by the Bybit API
21 /// is of the type BybitContentError.
22 #[error("Bybit error: {0}")]
23 BybitError(BybitContentError),
24
25 /// KlineValueMissingError variant that holds the index of the missing value, and the name of the missing value.
26 /// This variant is used when a value in a kline vector is missing.
27 #[error("Invalid Vec for Kline: {name} at {index} is missing")]
28 KlineValueMissingError { index: usize, name: &'static str },
29
30 /// Variants that hold the error returned by reqwest, serde_json, tokio_tungstenite, and std libraries.
31 /// These variants are used when the respective library returns an error.
32 #[error(transparent)]
33 ReqError(#[from] reqwest::Error),
34
35 #[error(transparent)]
36 InvalidHeaderError(#[from] reqwest::header::InvalidHeaderValue),
37
38 #[error(transparent)]
39 IoError(#[from] std::io::Error),
40
41 #[error(transparent)]
42 ParseFloatError(#[from] std::num::ParseFloatError),
43
44 #[error(transparent)]
45 UrlParserError(#[from] url::ParseError),
46
47 #[error(transparent)]
48 Json(#[from] serde_json::Error),
49
50 #[error(transparent)]
51 Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
52
53 #[error(transparent)]
54 TimestampError(#[from] std::time::SystemTimeError),
55
56 #[error(transparent)]
57 SerdeError(#[from] serde::de::value::Error),
58
59 // Variants representing common errors.
60 #[error("Internal Server Error")]
61 InternalServerError,
62
63 #[error("Service Unavailable")]
64 ServiceUnavailable,
65
66 #[error("Unauthorized")]
67 Unauthorized,
68
69 /// StatusCode variant that holds the status code.
70 #[error("Status Code")]
71 StatusCode(u16),
72
73 /// Base variant that holds a String representing the error.
74 /// This variant is used when the error is not of any specific type, and it is just a simple String.
75 #[error("Bybit error: {0}")]
76 Base(String),
77}
78
79// Implement the fmt::Display trait for BybitContentError.
80// This trait is used to specify how BybitContentError should be converted to a string.
81impl fmt::Display for BybitContentError {
82 // This method takes a mutable reference to a fmt::Formatter, and returns a fmt::Result.
83 // It writes the `msg` field of the BybitContentError struct to the given formatter.
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 // Implement this method to dictate how BybitContentError should be converted to a string.
86 // This is a placeholder implementation that simply writes the `msg` field; you should
87 // replace it with your own format string.
88 write!(f, "{}", self.msg)
89 }
90}
91
92// Implement the From trait for String and BybitError.
93// This trait is used to specify how a String can be converted to BybitError.
94impl From<std::string::String> for BybitError {
95 // This function takes a String, and returns a BybitError.
96 fn from(err: String) -> Self {
97 //
98 // Convert the String error to BybitError here
99 // For example, you can return a new instance of BybitError with the error message
100 BybitError::new(err)
101 }
102}
103
104impl BybitError {
105 fn new(arg: String) -> Self {
106 BybitError::Base(arg)
107 }
108}