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