switchgear_components/discovery/
error.rs

1use std::borrow::Cow;
2use std::fmt::{Display, Formatter};
3use switchgear_service_api::service::{HasServiceErrorSource, ServiceErrorSource};
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("database transaction error: {0}")]
32    Transaction(#[from] sea_orm::TransactionError<sea_orm::DbErr>),
33    #[error("deserialization failed: {0}")]
34    Deserialization(reqwest::Error),
35    #[error("HTTP request failed: {0}")]
36    Http(reqwest::Error),
37    #[error("HTTP status error: {0}")]
38    HttpStatus(u16),
39    #[error("I/O error: {0}")]
40    Io(#[from] std::io::Error),
41    #[error("JSON serialization error: {0}")]
42    JsonSerialization(#[from] serde_json::Error),
43    #[error("internal error: {0}")]
44    Internal(String),
45    #[error("Invalid Input error: {0}")]
46    InvalidInput(String),
47}
48
49impl DiscoveryBackendStoreError {
50    pub fn new<C: Into<Cow<'static, str>>>(
51        source: DiscoveryBackendStoreErrorSource,
52        esource: ServiceErrorSource,
53        context: C,
54    ) -> Self {
55        Self {
56            context: context.into(),
57            source,
58            esource,
59        }
60    }
61
62    pub fn from_sqlx<C: Into<Cow<'static, str>>>(
63        sqlx_error: sqlx::Error,
64        esource: ServiceErrorSource,
65        context: C,
66    ) -> Self {
67        Self {
68            context: context.into(),
69            source: DiscoveryBackendStoreErrorSource::Sqlx(sqlx_error),
70            esource,
71        }
72    }
73
74    pub fn from_db<C: Into<Cow<'static, str>>>(
75        esource: ServiceErrorSource,
76        context: C,
77        db_error: sea_orm::DbErr,
78    ) -> Self {
79        Self {
80            source: DiscoveryBackendStoreErrorSource::Database(db_error),
81            esource,
82            context: context.into(),
83        }
84    }
85
86    pub fn from_tx<C: Into<Cow<'static, str>>>(
87        esource: ServiceErrorSource,
88        context: C,
89        db_error: sea_orm::TransactionError<sea_orm::DbErr>,
90    ) -> Self {
91        Self {
92            source: DiscoveryBackendStoreErrorSource::Transaction(db_error),
93            esource,
94            context: context.into(),
95        }
96    }
97
98    pub fn deserialization_error<C: Into<Cow<'static, str>>>(
99        esource: ServiceErrorSource,
100        context: C,
101        original_error: reqwest::Error,
102    ) -> Self {
103        Self::new(
104            DiscoveryBackendStoreErrorSource::Deserialization(original_error),
105            esource,
106            context,
107        )
108    }
109
110    pub fn http_error<C: Into<Cow<'static, str>>>(
111        esource: ServiceErrorSource,
112        context: C,
113        original_error: reqwest::Error,
114    ) -> Self {
115        Self::new(
116            DiscoveryBackendStoreErrorSource::Http(original_error),
117            esource,
118            context,
119        )
120    }
121
122    pub fn http_status_error<C: Into<Cow<'static, str>>>(
123        esource: ServiceErrorSource,
124        context: C,
125        status_code: u16,
126    ) -> Self {
127        Self::new(
128            DiscoveryBackendStoreErrorSource::HttpStatus(status_code),
129            esource,
130            context,
131        )
132    }
133
134    pub fn io_error<C: Into<Cow<'static, str>>>(
135        esource: ServiceErrorSource,
136        context: C,
137        original_error: std::io::Error,
138    ) -> Self {
139        Self::new(
140            DiscoveryBackendStoreErrorSource::Io(original_error),
141            esource,
142            context,
143        )
144    }
145
146    pub fn json_serialization_error<C: Into<Cow<'static, str>>>(
147        esource: ServiceErrorSource,
148        context: C,
149        original_error: serde_json::Error,
150    ) -> Self {
151        Self::new(
152            DiscoveryBackendStoreErrorSource::JsonSerialization(original_error),
153            esource,
154            context,
155        )
156    }
157
158    pub fn internal_error<C: Into<Cow<'static, str>>>(
159        esource: ServiceErrorSource,
160        context: C,
161        message: String,
162    ) -> Self {
163        Self::new(
164            DiscoveryBackendStoreErrorSource::Internal(message),
165            esource,
166            context,
167        )
168    }
169
170    pub fn invalid_input_error<C: Into<Cow<'static, str>>>(context: C, message: String) -> Self {
171        Self::new(
172            DiscoveryBackendStoreErrorSource::InvalidInput(message),
173            ServiceErrorSource::Downstream,
174            context,
175        )
176    }
177
178    pub fn context(&self) -> &str {
179        self.context.as_ref()
180    }
181
182    pub fn source(&self) -> &DiscoveryBackendStoreErrorSource {
183        &self.source
184    }
185
186    pub fn esource(&self) -> ServiceErrorSource {
187        self.esource
188    }
189}
190
191impl HasServiceErrorSource for DiscoveryBackendStoreError {
192    fn get_service_error_source(&self) -> ServiceErrorSource {
193        self.esource
194    }
195}