scouter_error/
lib.rs

1use pyo3::create_exception;
2use pyo3::exceptions::PyException;
3use pyo3::PyErr;
4use serde::Deserialize;
5use thiserror::Error;
6
7#[derive(Error, Debug, PartialEq, Deserialize)]
8pub enum MonitorError {
9    #[error("{0}")]
10    CreateError(String),
11
12    #[error("Sample error: {0}")]
13    SampleDataError(String),
14
15    #[error("Compute error: {0}")]
16    ComputeError(String),
17
18    #[error("Shape mismatch: {0}")]
19    ShapeMismatchError(String),
20
21    #[error("Missing feature: {0}")]
22    MissingFeatureError(String),
23
24    #[error("Array Error: {0}")]
25    ArrayError(String),
26}
27
28#[derive(Error, Debug)]
29pub enum ProfilerError {
30    #[error("Quantile error: {0}")]
31    QuantileError(String),
32
33    #[error("Error calculating mean")]
34    MeanError,
35
36    #[error("Compute error: {0}")]
37    ComputeError(String),
38
39    #[error("Failed to compute string statistics")]
40    StringStatsError,
41
42    #[error("Failed to create feature map: {0}")]
43    FeatureMapError(String),
44
45    #[error("Array Error: {0}")]
46    ArrayError(String),
47
48    #[error("Failed to convert: {0}")]
49    ConversionError(String),
50
51    #[error("Failed to create string profile: {0}")]
52    StringProfileError(String),
53}
54
55#[derive(Error, Debug, Deserialize)]
56pub enum FeatureQueueError {
57    #[error("{0}")]
58    InvalidFormatError(String),
59
60    #[error("Failed to create drift record: {0}")]
61    DriftRecordError(String),
62
63    #[error("Failed to create alert record: {0}")]
64    AlertRecordError(String),
65
66    #[error("Failed to get feature")]
67    GetFeatureError,
68
69    #[error("Missing feature map")]
70    MissingFeatureMapError,
71
72    #[error("invalid data type detected for feature: {0}")]
73    InvalidFeatureTypeError(String),
74
75    #[error("invalid value detected for feature: {0}, error: {1}")]
76    InvalidValueError(String, String),
77
78    #[error("Failed to get bin given bin id")]
79    GetBinError,
80}
81
82// impl From for PyErr
83
84impl From<FeatureQueueError> for PyErr {
85    fn from(err: FeatureQueueError) -> PyErr {
86        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(err.to_string())
87    }
88}
89
90#[derive(Error, Debug, Deserialize)]
91pub enum SqlError {
92    #[error("Failed to run sql migrations: {0}")]
93    MigrationError(String),
94
95    #[error("Failed to run sql query: {0}")]
96    QueryError(String),
97
98    #[error("Failed to parse version: {0}")]
99    VersionError(String),
100
101    #[error("File error: {0}")]
102    FileError(String),
103
104    #[error("Error - {0}")]
105    GeneralError(String),
106
107    #[error("Failed to connect to the database - {0}")]
108    ConnectionError(String),
109}
110
111#[derive(Error, Debug, Deserialize)]
112pub enum AlertError {
113    #[error("Error: {0}")]
114    GeneralError(String),
115
116    #[error("Failed to create alert: {0}")]
117    CreateError(String),
118
119    #[error("{0}")]
120    DriftError(String),
121}
122
123#[derive(Error, Debug, Deserialize)]
124pub enum DriftError {
125    #[error("Error: {0}")]
126    Error(String),
127
128    #[error(transparent)]
129    AlertError(#[from] AlertError),
130
131    #[error(transparent)]
132    SqlError(#[from] SqlError),
133
134    #[error(transparent)]
135    MonitorError(#[from] MonitorError),
136}
137
138#[derive(Error, Debug, Deserialize)]
139pub enum ScouterError {
140    #[error("Failed to serialize string")]
141    SerializeError,
142
143    #[error("Failed to deserialize string")]
144    DeSerializeError,
145
146    #[error("Failed to create path")]
147    CreatePathError,
148
149    #[error("Failed to get parent path")]
150    GetParentPathError,
151
152    #[error("Failed to create directory")]
153    CreateDirectoryError,
154
155    #[error("Failed to write to file")]
156    WriteError,
157
158    #[error("Failed to read to file")]
159    ReadError,
160
161    #[error("Type error for {0}")]
162    TypeError(String),
163
164    #[error("Missing feature map")]
165    MissingFeatureMapError,
166
167    #[error("Failed to create string profile: {0}")]
168    StringProfileError(String),
169
170    #[error("Invalid drift type: {0}")]
171    InvalidDriftTypeError(String),
172
173    #[error("Shape mismatch: {0}")]
174    ShapeMismatchError(String),
175
176    #[error("{0}")]
177    FeatureError(String),
178
179    #[error("{0}")]
180    Error(String),
181
182    #[error(transparent)]
183    MonitorError(#[from] MonitorError),
184
185    #[error(transparent)]
186    AlertError(#[from] AlertError),
187
188    #[error(transparent)]
189    CustomError(#[from] CustomMetricError),
190
191    #[error(transparent)]
192    FeatureQueueError(#[from] FeatureQueueError),
193
194    #[error(transparent)]
195    SqlError(#[from] SqlError),
196
197    #[error(transparent)]
198    DriftError(#[from] DriftError),
199
200    #[error("Missing value in map")]
201    MissingValue,
202}
203
204// impl From for PyErr
205
206impl From<std::io::Error> for ScouterError {
207    fn from(err: std::io::Error) -> ScouterError {
208        ScouterError::Error(err.to_string())
209    }
210}
211
212impl From<PyErr> for ScouterError {
213    fn from(err: PyErr) -> ScouterError {
214        ScouterError::Error(err.to_string())
215    }
216}
217
218impl From<ScouterError> for PyErr {
219    fn from(err: ScouterError) -> PyErr {
220        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(err.to_string())
221    }
222}
223
224#[derive(Error, Debug)]
225pub enum DispatchError {
226    #[error("{0}")]
227    OpsGenieError(String),
228
229    #[error("{0}")]
230    SlackError(String),
231
232    #[error("{0}")]
233    HttpError(String),
234
235    #[error("Error processing alerts: {0}")]
236    AlertProcessError(String),
237
238    #[error("Error setting alerter: {0}")]
239    AlerterError(String),
240}
241
242#[derive(Error, Debug, Deserialize)]
243pub enum ObserverError {
244    #[error("Route not found {0}")]
245    RouteNotFound(String),
246
247    #[error("Failed to update route metrics: {0}")]
248    UpdateMetricsError(String),
249
250    #[error("Failed to compute quantiles: {0}")]
251    QuantileError(String),
252
253    #[error("Failed to collect metrics: {0}")]
254    CollectMetricsError(String),
255}
256
257impl From<ObserverError> for PyErr {
258    fn from(err: ObserverError) -> PyErr {
259        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(err.to_string())
260    }
261}
262
263#[derive(Error, Debug, Deserialize)]
264pub enum CustomMetricError {
265    #[error("Cannot create metric profile, no metrics were provided")]
266    NoMetricsError,
267
268    #[error("{0}")]
269    Error(String),
270}
271
272impl From<CustomMetricError> for PyErr {
273    fn from(err: CustomMetricError) -> PyErr {
274        PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(err.to_string())
275    }
276}
277
278#[derive(Error, Debug, Deserialize)]
279pub enum ConfigError {
280    #[error("{0}")]
281    Error(String),
282}
283
284#[derive(Error, Debug, Deserialize)]
285pub enum LoggingError {
286    #[error("{0}")]
287    Error(String),
288}
289
290#[derive(Error, Debug, Deserialize)]
291pub enum EventError {
292    #[error("Event error: {0}")]
293    Error(String),
294
295    // inherit SqlError
296    #[error(transparent)]
297    SqlError(#[from] SqlError),
298}
299
300create_exception!(scouter, PyScouterError, PyException);