Skip to main content

hyper_agent_core/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum HyperAgentError {
5    #[error("Strategy not found: {0}")]
6    StrategyNotFound(String),
7    #[error("Risk blocked: {0}")]
8    RiskBlocked(String),
9    #[error("Circuit breaker tripped")]
10    CircuitBreakerTripped,
11    #[error("Execution failed: {0}")]
12    ExecutionFailed(String),
13    #[error("Auth error: {0}")]
14    AuthError(String),
15    #[error("Config error: {0}")]
16    ConfigError(String),
17    #[error("Storage error: {0}")]
18    StorageError(String),
19    #[error("Exchange error: {0}")]
20    ExchangeError(String),
21    #[error("Connection error: {0}")]
22    ConnectionError(String),
23    #[error("{0}")]
24    Other(String),
25}
26
27pub type Result<T> = std::result::Result<T, HyperAgentError>;
28
29// Conversions
30impl From<crate::pipeline::PipelineError> for HyperAgentError {
31    fn from(e: crate::pipeline::PipelineError) -> Self {
32        match e {
33            crate::pipeline::PipelineError::RiskBlocked(msg) => Self::RiskBlocked(msg),
34            crate::pipeline::PipelineError::CircuitBreakerTripped => Self::CircuitBreakerTripped,
35            crate::pipeline::PipelineError::ExecutionFailed(msg) => Self::ExecutionFailed(msg),
36            crate::pipeline::PipelineError::AuthError(msg) => Self::AuthError(msg),
37            crate::pipeline::PipelineError::ConnectionError(msg) => Self::ConnectionError(msg),
38        }
39    }
40}
41
42impl From<crate::executor::ExecutorError> for HyperAgentError {
43    fn from(e: crate::executor::ExecutorError) -> Self {
44        Self::ExecutionFailed(e.to_string())
45    }
46}
47
48impl HyperAgentError {
49    pub fn exit_code(&self) -> i32 {
50        match self {
51            Self::RiskBlocked(_) | Self::CircuitBreakerTripped => 4,
52            Self::AuthError(_) => 3,
53            Self::ExecutionFailed(_) | Self::ExchangeError(_) | Self::ConnectionError(_) => 5,
54            Self::StrategyNotFound(_) => 6,
55            _ => 1,
56        }
57    }
58}