deribit_fix/error/
mod.rs

1//! Error types for the Deribit FIX framework
2
3use std::fmt;
4
5/// Result type alias for the Deribit FIX framework
6pub type Result<T> = std::result::Result<T, DeribitFixError>;
7
8/// Main error type for the Deribit FIX framework
9#[derive(Debug)]
10pub enum DeribitFixError {
11    /// Connection-related errors
12    Connection(String),
13    /// Authentication errors
14    Authentication(String),
15    /// Message parsing errors
16    MessageParsing(String),
17    /// Session management errors
18    Session(String),
19    /// Network I/O errors
20    Io(std::io::Error),
21    /// JSON serialization/deserialization errors
22    Json(serde_json::Error),
23    /// HTTP request errors
24    Http(reqwest::Error),
25    /// Configuration errors
26    Config(String),
27    /// Timeout errors
28    Timeout(String),
29    /// Protocol violation errors
30    Protocol(String),
31    /// Generic errors
32    Generic(String),
33}
34
35impl fmt::Display for DeribitFixError {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            DeribitFixError::Connection(msg) => write!(f, "Connection error: {}", msg),
39            DeribitFixError::Authentication(msg) => write!(f, "Authentication error: {}", msg),
40            DeribitFixError::MessageParsing(msg) => write!(f, "Message parsing error: {}", msg),
41            DeribitFixError::Session(msg) => write!(f, "Session error: {}", msg),
42            DeribitFixError::Io(err) => write!(f, "I/O error: {}", err),
43            DeribitFixError::Json(err) => write!(f, "JSON error: {}", err),
44            DeribitFixError::Http(err) => write!(f, "HTTP error: {}", err),
45            DeribitFixError::Config(msg) => write!(f, "Configuration error: {}", msg),
46            DeribitFixError::Timeout(msg) => write!(f, "Timeout error: {}", msg),
47            DeribitFixError::Protocol(msg) => write!(f, "Protocol error: {}", msg),
48            DeribitFixError::Generic(msg) => write!(f, "Error: {}", msg),
49        }
50    }
51}
52
53impl std::error::Error for DeribitFixError {
54    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
55        match self {
56            DeribitFixError::Io(err) => Some(err),
57            DeribitFixError::Json(err) => Some(err),
58            DeribitFixError::Http(err) => Some(err),
59            _ => None,
60        }
61    }
62}
63
64impl From<std::io::Error> for DeribitFixError {
65    fn from(err: std::io::Error) -> Self {
66        DeribitFixError::Io(err)
67    }
68}
69
70impl From<serde_json::Error> for DeribitFixError {
71    fn from(err: serde_json::Error) -> Self {
72        DeribitFixError::Json(err)
73    }
74}
75
76impl From<reqwest::Error> for DeribitFixError {
77    fn from(err: reqwest::Error) -> Self {
78        DeribitFixError::Http(err)
79    }
80}