switchgear_service/components/offer/
error.rs1use crate::api::service::{HasServiceErrorSource, ServiceErrorSource};
2use std::borrow::Cow;
3use std::fmt::{Display, Formatter};
4use thiserror::Error;
5
6#[derive(Error, Debug)]
7pub struct OfferStoreError {
8 context: Cow<'static, str>,
9 #[source]
10 source: OfferStoreErrorSourceKind,
11 esource: ServiceErrorSource,
12}
13
14impl Display for OfferStoreError {
15 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16 write!(
17 f,
18 "OfferStoreError: while {}: {}",
19 self.context.as_ref(),
20 self.source
21 )
22 }
23}
24
25#[derive(Error, Debug)]
26pub enum OfferStoreErrorSourceKind {
27 #[error("database error: {0}")]
28 Database(#[from] sea_orm::DbErr),
29 #[error("resource not found")]
30 NotFound,
31 #[error("serialization failed: {0}")]
32 Serialization(serde_json::Error),
33 #[error("deserialization failed: {0}")]
34 Deserialization(reqwest::Error),
35 #[error("hash conversion failed")]
36 HashConversion,
37 #[error("HTTP request failed: {0}")]
38 Http(reqwest::Error),
39 #[error("HTTP status error: {0}")]
40 HttpStatus(u16),
41 #[error("Internal error: {0}")]
42 Internal(String),
43}
44
45impl OfferStoreError {
46 fn new<C: Into<Cow<'static, str>>>(
47 source: OfferStoreErrorSourceKind,
48 esource: ServiceErrorSource,
49 context: C,
50 ) -> Self {
51 Self {
52 context: context.into(),
53 source,
54 esource,
55 }
56 }
57
58 pub fn not_found<C: Into<Cow<'static, str>>>(esource: ServiceErrorSource, context: C) -> Self {
60 Self::new(OfferStoreErrorSourceKind::NotFound, esource, context)
61 }
62
63 pub fn serialization_error<C: Into<Cow<'static, str>>>(
64 esource: ServiceErrorSource,
65 context: C,
66 original_error: serde_json::Error,
67 ) -> Self {
68 Self::new(
69 OfferStoreErrorSourceKind::Serialization(original_error),
70 esource,
71 context,
72 )
73 }
74
75 pub fn hash_conversion_error<C: Into<Cow<'static, str>>>(
76 esource: ServiceErrorSource,
77 context: C,
78 ) -> Self {
79 Self::new(OfferStoreErrorSourceKind::HashConversion, esource, context)
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 OfferStoreErrorSourceKind::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 OfferStoreErrorSourceKind::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 OfferStoreErrorSourceKind::HttpStatus(status_code),
113 esource,
114 context,
115 )
116 }
117
118 pub fn internal_error<C: Into<Cow<'static, str>>>(
119 esource: ServiceErrorSource,
120 context: C,
121 message: String,
122 ) -> Self {
123 Self::new(
124 OfferStoreErrorSourceKind::Internal(message),
125 esource,
126 context,
127 )
128 }
129
130 pub fn from_db<C: Into<Cow<'static, str>>>(
131 esource: ServiceErrorSource,
132 context: C,
133 db_error: sea_orm::DbErr,
134 ) -> Self {
135 Self {
136 source: OfferStoreErrorSourceKind::Database(db_error),
137 esource,
138 context: context.into(),
139 }
140 }
141
142 pub fn context(&self) -> &str {
143 self.context.as_ref()
144 }
145
146 pub fn source(&self) -> &OfferStoreErrorSourceKind {
147 &self.source
148 }
149
150 pub fn esource(&self) -> ServiceErrorSource {
151 self.esource
152 }
153}
154
155impl HasServiceErrorSource for OfferStoreError {
156 fn get_service_error_source(&self) -> ServiceErrorSource {
157 self.esource
158 }
159}