1use crate::types::ResourceId;
4
5pub type LighthouseResult<T> = Result<T, LighthouseError>;
7
8#[derive(thiserror::Error, Debug)]
10pub enum LighthouseError {
11 #[error("Configuration error: {message}")]
13 Config { message: String },
14
15 #[error("Resource '{resource_id}' not found or invalid")]
17 ResourceNotFound { resource_id: ResourceId },
18
19 #[error("Invalid metric data: {message}")]
21 InvalidMetric { message: String },
22
23 #[error("Callback execution failed for '{operation}': {message}")]
25 CallbackFailed { operation: String, message: String },
26
27 #[error("Lighthouse engine is not running: {message}")]
29 EngineNotRunning { message: String },
30
31 #[error("Failed to evaluate scaling policy '{policy_name}': {message}")]
33 PolicyEvaluation { policy_name: String, message: String },
34
35 #[error("Internal channel error: {message}")]
37 ChannelError { message: String },
38
39 #[error("Serialization error: {source}")]
41 Serialization {
42 #[from]
43 source: serde_json::Error,
44 },
45
46 #[error("IO error: {source}")]
48 Io {
49 #[from]
50 source: std::io::Error,
51 },
52
53 #[error("Unexpected error: {message}")]
55 Unexpected { message: String },
56}
57
58impl 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
105impl<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
114impl 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}