1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum VeloraError {
8 #[error("Data error: {0}")]
11 DataError(String),
12
13 #[error("Invalid market data: {0}")]
15 InvalidMarketData(String),
16
17 #[error("Data not found: {0}")]
19 DataNotFound(String),
20
21 #[error("Exchange error: {0}")]
24 ExchangeError(String),
25
26 #[error("Connection error: {0}")]
28 ConnectionError(String),
29
30 #[error("Authentication error: {0}")]
32 AuthenticationError(String),
33
34 #[error("Rate limit exceeded")]
36 RateLimitExceeded,
37
38 #[error("Invalid API response: {0}")]
40 InvalidResponse(String),
41
42 #[error("Order error: {0}")]
45 OrderError(String),
46
47 #[error("Insufficient balance: {0}")]
49 InsufficientBalance(String),
50
51 #[error("Invalid order: {0}")]
53 InvalidOrder(String),
54
55 #[error("Order not found: {0}")]
57 OrderNotFound(String),
58
59 #[error("Strategy error: {0}")]
62 StrategyError(String),
63
64 #[error("Invalid strategy configuration: {0}")]
66 InvalidStrategyConfig(String),
67
68 #[error("Risk limit exceeded: {0}")]
71 RiskLimitExceeded(String),
72
73 #[error("Position limit exceeded: {0}")]
75 PositionLimitExceeded(String),
76
77 #[error("Configuration error: {0}")]
80 ConfigError(String),
81
82 #[error("Missing configuration: {0}")]
84 MissingConfig(String),
85
86 #[error("IO error: {0}")]
89 IoError(#[from] std::io::Error),
90
91 #[error("Serialization error: {0}")]
93 SerializationError(String),
94
95 #[error("Parse error: {0}")]
97 ParseError(String),
98
99 #[error("Internal error: {0}")]
102 InternalError(String),
103
104 #[error("Not implemented: {0}")]
106 NotImplemented(String),
107
108 #[error("Invalid parameter: {0}")]
110 InvalidParameter(String),
111}
112
113impl 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
132pub 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}