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    /// Message construction errors
18    MessageConstruction(String),
19    /// Session management errors
20    Session(String),
21    /// Network I/O errors
22    Io(std::io::Error),
23    /// JSON serialization/deserialization errors
24    Json(serde_json::Error),
25    /// HTTP request errors
26    Http(reqwest::Error),
27    /// Configuration errors
28    Config(String),
29    /// Timeout errors
30    Timeout(String),
31    /// Protocol violation errors
32    Protocol(String),
33    /// Generic errors
34    Generic(String),
35}
36
37impl fmt::Display for DeribitFixError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            DeribitFixError::Connection(msg) => write!(f, "Connection error: {msg}"),
41            DeribitFixError::Authentication(msg) => write!(f, "Authentication error: {msg}"),
42            DeribitFixError::MessageParsing(msg) => write!(f, "Message parsing error: {msg}"),
43            DeribitFixError::MessageConstruction(msg) => {
44                write!(f, "Message construction error: {msg}")
45            }
46            DeribitFixError::Session(msg) => write!(f, "Session error: {msg}"),
47            DeribitFixError::Io(err) => write!(f, "I/O error: {err}"),
48            DeribitFixError::Json(err) => write!(f, "JSON error: {err}"),
49            DeribitFixError::Http(err) => write!(f, "HTTP error: {err}"),
50            DeribitFixError::Config(msg) => write!(f, "Configuration error: {msg}"),
51            DeribitFixError::Timeout(msg) => write!(f, "Timeout error: {msg}"),
52            DeribitFixError::Protocol(msg) => write!(f, "Protocol error: {msg}"),
53            DeribitFixError::Generic(msg) => write!(f, "Error: {msg}"),
54        }
55    }
56}
57
58impl std::error::Error for DeribitFixError {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            DeribitFixError::Io(err) => Some(err),
62            DeribitFixError::Json(err) => Some(err),
63            DeribitFixError::Http(err) => Some(err),
64            _ => None,
65        }
66    }
67}
68
69impl From<std::io::Error> for DeribitFixError {
70    fn from(err: std::io::Error) -> Self {
71        DeribitFixError::Io(err)
72    }
73}
74
75impl From<serde_json::Error> for DeribitFixError {
76    fn from(err: serde_json::Error) -> Self {
77        DeribitFixError::Json(err)
78    }
79}
80
81impl From<reqwest::Error> for DeribitFixError {
82    fn from(err: reqwest::Error) -> Self {
83        DeribitFixError::Http(err)
84    }
85}