lighthouse/
error.rs

1// src/error.rs
2
3use crate::types::ResourceId;
4
5/// Result type used throughout the lighthouse library
6pub type LighthouseResult<T> = Result<T, LighthouseError>;
7
8/// All possible errors that can occur in the lighthouse library
9#[derive(thiserror::Error, Debug)]
10pub enum LighthouseError {
11    /// Configuration-related errors
12    #[error("Configuration error: {message}")]
13    Config { message: String },
14
15    /// Resource not found or invalid
16    #[error("Resource '{resource_id}' not found or invalid")]
17    ResourceNotFound { resource_id: ResourceId },
18
19    /// Invalid or missing metrics
20    #[error("Invalid metric data: {message}")]
21    InvalidMetric { message: String },
22
23    /// Callback execution failed
24    #[error("Callback execution failed for '{operation}': {message}")]
25    CallbackFailed { operation: String, message: String },
26
27    /// Engine is not running or has stopped
28    #[error("Lighthouse engine is not running: {message}")]
29    EngineNotRunning { message: String },
30
31    /// Policy evaluation failed
32    #[error("Failed to evaluate scaling policy '{policy_name}': {message}")]
33    PolicyEvaluation { policy_name: String, message: String },
34
35    /// Channel communication error (internal)
36    #[error("Internal channel error: {message}")]
37    ChannelError { message: String },
38
39    /// Serialization/deserialization errors
40    #[error("Serialization error: {source}")]
41    Serialization {
42        #[from]
43        source: serde_json::Error,
44    },
45
46    /// IO-related errors
47    #[error("IO error: {source}")]
48    Io {
49        #[from]
50        source: std::io::Error,
51    },
52
53    /// Generic error for unexpected situations
54    #[error("Unexpected error: {message}")]
55    Unexpected { message: String },
56}
57
58/// Helper methods for creating common errors
59impl LighthouseError {
60    pub fn config<S: Into<String>>(message: S) -> Self {
61        Self::Config {
62            message: message.into(),
63        }
64    }
65
66    pub fn resource_not_found<S: Into<String>>(resource_id: S) -> Self {
67        Self::ResourceNotFound {
68            resource_id: resource_id.into(),
69        }
70    }
71
72    pub fn invalid_metric<S: Into<String>>(message: S) -> Self {
73        Self::InvalidMetric {
74            message: message.into(),
75        }
76    }
77
78    pub fn callback_failed<S: Into<String>>(operation: S, message: S) -> Self {
79        Self::CallbackFailed {
80            operation: operation.into(),
81            message: message.into(),
82        }
83    }
84
85    pub fn engine_not_running<S: Into<String>>(message: S) -> Self {
86        Self::EngineNotRunning {
87            message: message.into(),
88        }
89    }
90
91    pub fn policy_evaluation<S: Into<String>>(policy_name: S, message: S) -> Self {
92        Self::PolicyEvaluation {
93            policy_name: policy_name.into(),
94            message: message.into(),
95        }
96    }
97
98    pub fn unexpected<S: Into<String>>(message: S) -> Self {
99        Self::Unexpected {
100            message: message.into(),
101        }
102    }
103}
104
105/// Convert from channel send errors
106impl<T> From<tokio::sync::mpsc::error::SendError<T>> for LighthouseError {
107    fn from(error: tokio::sync::mpsc::error::SendError<T>) -> Self {
108        Self::ChannelError {
109            message: format!("Failed to send on channel: {}", error),
110        }
111    }
112}
113
114/// Convert from channel receive errors  
115impl From<tokio::sync::oneshot::error::RecvError> for LighthouseError {
116    fn from(error: tokio::sync::oneshot::error::RecvError) -> Self {
117        Self::ChannelError {
118            message: format!("Failed to receive on channel: {}", error),
119        }
120    }
121}