deribit_base/error/
types.rs1use std::fmt;
4
5#[derive(Debug, Clone)]
7pub enum DeribitError {
8 Connection(String),
10 Authentication(String),
12 Api {
14 code: i32,
16 message: String,
18 },
19 Serialization(String),
21 Timeout,
23 InvalidConfig(String),
25 Other(String),
27}
28
29impl fmt::Display for DeribitError {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 DeribitError::Connection(msg) => write!(f, "Connection error: {msg}"),
33 DeribitError::Authentication(msg) => write!(f, "Authentication error: {msg}"),
34 DeribitError::Api { code, message } => write!(f, "API error {code}: {message}"),
35 DeribitError::Serialization(msg) => write!(f, "Serialization error: {msg}"),
36 DeribitError::Timeout => write!(f, "Request timeout"),
37 DeribitError::InvalidConfig(msg) => write!(f, "Invalid configuration: {msg}"),
38 DeribitError::Other(msg) => write!(f, "Error: {msg}"),
39 }
40 }
41}
42
43impl std::error::Error for DeribitError {}
44
45pub type DeribitResult<T> = Result<T, DeribitError>;
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_deribit_error_connection() {
54 let error = DeribitError::Connection("Failed to connect".to_string());
55 assert_eq!(error.to_string(), "Connection error: Failed to connect");
56 }
57
58 #[test]
59 fn test_deribit_error_authentication() {
60 let error = DeribitError::Authentication("Invalid credentials".to_string());
61 assert_eq!(
62 error.to_string(),
63 "Authentication error: Invalid credentials"
64 );
65 }
66
67 #[test]
68 fn test_deribit_error_api() {
69 let error = DeribitError::Api {
70 code: 10009,
71 message: "Invalid request".to_string(),
72 };
73 assert_eq!(error.to_string(), "API error 10009: Invalid request");
74 }
75
76 #[test]
77 fn test_deribit_error_serialization() {
78 let error = DeribitError::Serialization("JSON parse error".to_string());
79 assert_eq!(error.to_string(), "Serialization error: JSON parse error");
80 }
81
82 #[test]
83 fn test_deribit_error_timeout() {
84 let error = DeribitError::Timeout;
85 assert_eq!(error.to_string(), "Request timeout");
86 }
87
88 #[test]
89 fn test_deribit_error_invalid_config() {
90 let error = DeribitError::InvalidConfig("Missing API key".to_string());
91 assert_eq!(error.to_string(), "Invalid configuration: Missing API key");
92 }
93
94 #[test]
95 fn test_deribit_error_other() {
96 let error = DeribitError::Other("Unknown error".to_string());
97 assert_eq!(error.to_string(), "Error: Unknown error");
98 }
99
100 #[test]
101 fn test_deribit_error_debug() {
102 let error = DeribitError::Connection("test".to_string());
103 let debug_str = format!("{:?}", error);
104 assert!(debug_str.contains("Connection"));
105 assert!(debug_str.contains("test"));
106 }
107
108 #[test]
109 fn test_deribit_error_clone() {
110 let error = DeribitError::Api {
111 code: 123,
112 message: "test".to_string(),
113 };
114 let cloned = error.clone();
115 assert_eq!(error.to_string(), cloned.to_string());
116 }
117
118 #[test]
119 fn test_deribit_result_type() {
120 let success: DeribitResult<i32> = Ok(42);
121 let failure: DeribitResult<i32> = Err(DeribitError::Timeout);
122
123 assert!(success.is_ok());
124 assert!(failure.is_err());
125 if let Ok(value) = success {
126 assert_eq!(value, 42);
127 }
128 }
129
130 #[test]
131 fn test_error_trait_implementation() {
132 let error = DeribitError::Connection("test".to_string());
133 let _: &dyn std::error::Error = &error;
134 }
135}