switchgear_service/components/discovery/
error.rs

1use crate::api::service::{HasServiceErrorSource, ServiceErrorSource};
2use std::borrow::Cow;
3use std::fmt::{Display, Formatter};
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub struct DiscoveryBackendStoreError {
8    context: Cow<'static, str>,
9    #[source]
10    source: DiscoveryBackendStoreErrorSource,
11    esource: ServiceErrorSource,
12}
13
14impl Display for DiscoveryBackendStoreError {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        write!(
17            f,
18            "DiscoveryBackendStoreError: while {}: {}",
19            self.context.as_ref(),
20            self.source
21        )
22    }
23}
24
25#[derive(Error, Debug)]
26pub enum DiscoveryBackendStoreErrorSource {
27    #[error("database error: {0}")]
28    Sqlx(#[from] sqlx::Error),
29    #[error("database error: {0}")]
30    Database(#[from] sea_orm::DbErr),
31    #[error("deserialization failed: {0}")]
32    Deserialization(reqwest::Error),
33    #[error("HTTP request failed: {0}")]
34    Http(reqwest::Error),
35    #[error("HTTP status error: {0}")]
36    HttpStatus(u16),
37    #[error("I/O error: {0}")]
38    Io(#[from] std::io::Error),
39    #[error("JSON serialization error: {0}")]
40    JsonSerialization(#[from] serde_json::Error),
41    #[error("internal error: {0}")]
42    Internal(String),
43}
44
45impl DiscoveryBackendStoreError {
46    pub fn new<C: Into<Cow<'static, str>>>(
47        source: DiscoveryBackendStoreErrorSource,
48        esource: ServiceErrorSource,
49        context: C,
50    ) -> Self {
51        Self {
52            context: context.into(),
53            source,
54            esource,
55        }
56    }
57
58    pub fn from_sqlx<C: Into<Cow<'static, str>>>(
59        sqlx_error: sqlx::Error,
60        esource: ServiceErrorSource,
61        context: C,
62    ) -> Self {
63        Self {
64            context: context.into(),
65            source: DiscoveryBackendStoreErrorSource::Sqlx(sqlx_error),
66            esource,
67        }
68    }
69
70    pub fn from_db<C: Into<Cow<'static, str>>>(
71        esource: ServiceErrorSource,
72        context: C,
73        db_error: sea_orm::DbErr,
74    ) -> Self {
75        Self {
76            source: DiscoveryBackendStoreErrorSource::Database(db_error),
77            esource,
78            context: context.into(),
79        }
80    }
81
82    pub fn deserialization_error<C: Into<Cow<'static, str>>>(
83        esource: ServiceErrorSource,
84        context: C,
85        original_error: reqwest::Error,
86    ) -> Self {
87        Self::new(
88            DiscoveryBackendStoreErrorSource::Deserialization(original_error),
89            esource,
90            context,
91        )
92    }
93
94    pub fn http_error<C: Into<Cow<'static, str>>>(
95        esource: ServiceErrorSource,
96        context: C,
97        original_error: reqwest::Error,
98    ) -> Self {
99        Self::new(
100            DiscoveryBackendStoreErrorSource::Http(original_error),
101            esource,
102            context,
103        )
104    }
105
106    pub fn http_status_error<C: Into<Cow<'static, str>>>(
107        esource: ServiceErrorSource,
108        context: C,
109        status_code: u16,
110    ) -> Self {
111        Self::new(
112            DiscoveryBackendStoreErrorSource::HttpStatus(status_code),
113            esource,
114            context,
115        )
116    }
117
118    pub fn io_error<C: Into<Cow<'static, str>>>(
119        esource: ServiceErrorSource,
120        context: C,
121        original_error: std::io::Error,
122    ) -> Self {
123        Self::new(
124            DiscoveryBackendStoreErrorSource::Io(original_error),
125            esource,
126            context,
127        )
128    }
129
130    pub fn json_serialization_error<C: Into<Cow<'static, str>>>(
131        esource: ServiceErrorSource,
132        context: C,
133        original_error: serde_json::Error,
134    ) -> Self {
135        Self::new(
136            DiscoveryBackendStoreErrorSource::JsonSerialization(original_error),
137            esource,
138            context,
139        )
140    }
141
142    pub fn internal_error<C: Into<Cow<'static, str>>>(
143        esource: ServiceErrorSource,
144        context: C,
145        message: String,
146    ) -> Self {
147        Self::new(
148            DiscoveryBackendStoreErrorSource::Internal(message),
149            esource,
150            context,
151        )
152    }
153
154    pub fn context(&self) -> &str {
155        self.context.as_ref()
156    }
157
158    pub fn source(&self) -> &DiscoveryBackendStoreErrorSource {
159        &self.source
160    }
161
162    pub fn esource(&self) -> ServiceErrorSource {
163        self.esource
164    }
165}
166
167impl HasServiceErrorSource for DiscoveryBackendStoreError {
168    fn get_service_error_source(&self) -> ServiceErrorSource {
169        self.esource
170    }
171}