rusty_common/
error.rs

1use crate::SmartString;
2use thiserror::Error;
3
4/// Common error type for the rusty-common crate
5#[non_exhaustive]
6#[derive(Error, Debug, Clone)]
7pub enum CommonError {
8    #[error("Authentication error: {0}")]
9    /// An error that occurred during authentication.
10    Auth(SmartString),
11
12    #[error("Network error: {0}")]
13    /// An error that occurred during a network operation.
14    Network(SmartString),
15
16    #[error("JSON parsing error: {0}")]
17    /// An error that occurred during JSON serialization or deserialization.
18    Json(SmartString),
19
20    #[error("Invalid parameter: {0}")]
21    /// An error that occurred due to an invalid parameter.
22    InvalidParameter(SmartString),
23
24    #[error("Parsing error: {0}")]
25    /// An error that occurred during parsing.
26    Parse(SmartString),
27
28    #[error("Internal error: {0}")]
29    /// An internal error.
30    Internal(SmartString),
31
32    #[error("Configuration error: {0}")]
33    /// An error that occurred due to a configuration issue.
34    Config(SmartString),
35
36    #[error("Websocket error: {0}")]
37    /// An error that occurred with a WebSocket connection.
38    Websocket(SmartString),
39
40    #[error("HTTP error: {0}")]
41    /// An error that occurred during an HTTP request.
42    Http(SmartString),
43
44    #[error("API error: {0}")]
45    /// An error that occurred in an exchange API.
46    Api(SmartString),
47}
48
49/// Result type alias for CommonError
50pub type Result<T> = std::result::Result<T, CommonError>;
51
52// Conversion implementations
53impl From<simd_json::Error> for CommonError {
54    fn from(err: simd_json::Error) -> Self {
55        Self::Json(err.to_string().into())
56    }
57}
58
59impl From<reqwest::Error> for CommonError {
60    fn from(err: reqwest::Error) -> Self {
61        Self::Network(err.to_string().into())
62    }
63}
64
65impl From<std::io::Error> for CommonError {
66    fn from(err: std::io::Error) -> Self {
67        Self::Internal(err.to_string().into())
68    }
69}
70
71impl From<url::ParseError> for CommonError {
72    fn from(err: url::ParseError) -> Self {
73        Self::Parse(err.to_string().into())
74    }
75}
76
77impl From<reqwest::header::InvalidHeaderValue> for CommonError {
78    fn from(err: reqwest::header::InvalidHeaderValue) -> Self {
79        Self::Http(err.to_string().into())
80    }
81}
82
83impl From<rust_decimal::Error> for CommonError {
84    fn from(err: rust_decimal::Error) -> Self {
85        Self::Parse(err.to_string().into())
86    }
87}