Skip to main content

velora_core/
errors.rs

1//! Error types for the Velora trading platform.
2
3use thiserror::Error;
4
5/// Main error type for the Velora platform.
6#[derive(Error, Debug)]
7pub enum VeloraError {
8    // Data errors
9    /// General data error
10    #[error("Data error: {0}")]
11    DataError(String),
12
13    /// Invalid market data received
14    #[error("Invalid market data: {0}")]
15    InvalidMarketData(String),
16
17    /// Requested data not found
18    #[error("Data not found: {0}")]
19    DataNotFound(String),
20
21    // Exchange errors
22    /// General exchange error
23    #[error("Exchange error: {0}")]
24    ExchangeError(String),
25
26    /// Connection error to exchange
27    #[error("Connection error: {0}")]
28    ConnectionError(String),
29
30    /// Authentication error with exchange
31    #[error("Authentication error: {0}")]
32    AuthenticationError(String),
33
34    /// Exchange rate limit exceeded
35    #[error("Rate limit exceeded")]
36    RateLimitExceeded,
37
38    /// Invalid response from exchange API
39    #[error("Invalid API response: {0}")]
40    InvalidResponse(String),
41
42    // Order errors
43    /// General order error
44    #[error("Order error: {0}")]
45    OrderError(String),
46
47    /// Insufficient balance for order
48    #[error("Insufficient balance: {0}")]
49    InsufficientBalance(String),
50
51    /// Invalid order parameters
52    #[error("Invalid order: {0}")]
53    InvalidOrder(String),
54
55    /// Order not found
56    #[error("Order not found: {0}")]
57    OrderNotFound(String),
58
59    // Strategy errors
60    /// General strategy error
61    #[error("Strategy error: {0}")]
62    StrategyError(String),
63
64    /// Invalid strategy configuration
65    #[error("Invalid strategy configuration: {0}")]
66    InvalidStrategyConfig(String),
67
68    // Risk management errors
69    /// Risk limit exceeded
70    #[error("Risk limit exceeded: {0}")]
71    RiskLimitExceeded(String),
72
73    /// Position limit exceeded
74    #[error("Position limit exceeded: {0}")]
75    PositionLimitExceeded(String),
76
77    // Configuration errors
78    /// General configuration error
79    #[error("Configuration error: {0}")]
80    ConfigError(String),
81
82    /// Missing required configuration
83    #[error("Missing configuration: {0}")]
84    MissingConfig(String),
85
86    // IO and serialization errors
87    /// IO error
88    #[error("IO error: {0}")]
89    IoError(#[from] std::io::Error),
90
91    /// Serialization error
92    #[error("Serialization error: {0}")]
93    SerializationError(String),
94
95    /// Parse error
96    #[error("Parse error: {0}")]
97    ParseError(String),
98
99    // General errors
100    /// Internal error (should not happen in normal operation)
101    #[error("Internal error: {0}")]
102    InternalError(String),
103
104    /// Not implemented feature
105    #[error("Not implemented: {0}")]
106    NotImplemented(String),
107
108    /// Invalid parameter
109    #[error("Invalid parameter: {0}")]
110    InvalidParameter(String),
111}
112
113// Implement conversions from common error types
114impl From<serde_json::Error> for VeloraError {
115    fn from(err: serde_json::Error) -> Self {
116        VeloraError::SerializationError(err.to_string())
117    }
118}
119
120impl From<config::ConfigError> for VeloraError {
121    fn from(err: config::ConfigError) -> Self {
122        VeloraError::ConfigError(err.to_string())
123    }
124}
125
126impl From<gonfig::Error> for VeloraError {
127    fn from(err: gonfig::Error) -> Self {
128        VeloraError::ConfigError(err.to_string())
129    }
130}
131
132/// Result type alias for Velora operations.
133pub type Result<T> = std::result::Result<T, VeloraError>;
134
135#[cfg(test)]
136mod tests {
137    use super::*;
138
139    #[test]
140    fn test_error_display() {
141        let error = VeloraError::InvalidOrder("Price must be positive".to_string());
142        assert_eq!(error.to_string(), "Invalid order: Price must be positive");
143    }
144
145    #[test]
146    fn test_error_from_io() {
147        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
148        let velora_error: VeloraError = io_error.into();
149        assert!(matches!(velora_error, VeloraError::IoError(_)));
150    }
151
152    #[test]
153    fn test_result_type() {
154        fn returns_result() -> Result<i32> {
155            Ok(42)
156        }
157
158        assert_eq!(returns_result().unwrap(), 42);
159    }
160}