openstack_keystone_core/
error.rs1use thiserror::Error;
18
19use crate::application_credential::error::ApplicationCredentialProviderError;
20use crate::assignment::error::AssignmentProviderError;
21use crate::catalog::error::CatalogProviderError;
22use crate::federation::error::FederationProviderError;
23use crate::identity::error::IdentityProviderError;
24use crate::identity_mapping::error::IdentityMappingProviderError;
25use crate::k8s_auth::error::K8sAuthProviderError;
26use crate::policy::PolicyError;
27use crate::resource::error::ResourceProviderError;
28use crate::revoke::error::RevokeProviderError;
29use crate::role::error::RoleProviderError;
30use crate::token::TokenProviderError;
31use crate::trust::TrustProviderError;
32#[derive(Debug, Error)]
36pub enum KeystoneError {
37 #[error(transparent)]
39 ApplicationCredential {
40 #[from]
42 source: ApplicationCredentialProviderError,
43 },
44
45 #[error(transparent)]
47 AssignmentProvider {
48 #[from]
50 source: AssignmentProviderError,
51 },
52
53 #[error(transparent)]
55 CatalogProvider {
56 #[from]
58 source: CatalogProviderError,
59 },
60
61 #[error(transparent)]
63 FederationProvider {
64 #[from]
66 source: FederationProviderError,
67 },
68
69 #[error(transparent)]
71 IdentityProvider {
72 #[from]
74 source: IdentityProviderError,
75 },
76
77 #[error(transparent)]
79 IdentityMapping {
80 #[from]
82 source: IdentityMappingProviderError,
83 },
84
85 #[error(transparent)]
87 IO {
88 #[from]
90 source: std::io::Error,
91 },
92
93 #[error("json serde error: {}", source)]
95 Json {
96 #[from]
98 source: serde_json::Error,
99 },
100
101 #[error(transparent)]
103 K8sAuthProvider {
104 #[from]
106 source: K8sAuthProviderError,
107 },
108
109 #[error(transparent)]
111 Policy {
112 #[from]
114 source: PolicyError,
115 },
116
117 #[error("policy enforcement is requested, but not available with the enabled features")]
119 PolicyEnforcementNotAvailable,
120
121 #[error(transparent)]
123 ResourceProvider {
124 #[from]
126 source: ResourceProviderError,
127 },
128
129 #[error(transparent)]
131 RevokeProvider {
132 #[from]
134 source: RevokeProviderError,
135 },
136
137 #[error(transparent)]
139 RoleProvider {
140 #[from]
142 source: RoleProviderError,
143 },
144
145 #[error(transparent)]
147 TokenProvider {
148 #[from]
150 source: TokenProviderError,
151 },
152
153 #[error(transparent)]
155 TrustProvider {
156 #[from]
158 source: TrustProviderError,
159 },
160
161 #[error(transparent)]
163 UrlParse {
164 #[from]
165 source: url::ParseError,
166 },
167
168 #[error("provider error: {}", source)]
169 Provider {
170 #[source]
171 source: Box<dyn std::error::Error + Send + Sync + 'static>,
172 },
173 }
181
182#[derive(Debug, Error)]
187#[non_exhaustive]
188pub enum BuilderError {
189 #[error("{0}")]
191 UninitializedField(String),
192 #[error("{0}")]
194 Validation(String),
195}
196
197impl From<String> for BuilderError {
198 fn from(s: String) -> Self {
199 Self::Validation(s)
200 }
201}
202
203impl From<openstack_keystone_api_types::error::BuilderError> for BuilderError {
204 fn from(value: openstack_keystone_api_types::error::BuilderError) -> Self {
205 match value {
206 openstack_keystone_api_types::error::BuilderError::UninitializedField(e) => {
207 Self::UninitializedField(e)
208 }
209 openstack_keystone_api_types::error::BuilderError::Validation(e) => Self::Validation(e),
210 }
211 }
212}
213
214impl From<derive_builder::UninitializedFieldError> for BuilderError {
215 fn from(ufe: derive_builder::UninitializedFieldError) -> Self {
216 Self::UninitializedField(ufe.to_string())
217 }
218}
219
220#[derive(Debug, Error)]
222pub enum DatabaseError {
223 #[error("{message} while {context}")]
225 Conflict {
226 message: String,
228 context: String,
230 },
231
232 #[error("Database error {source} while {context}")]
234 Database {
235 source: sea_orm::DbErr,
237 context: String,
239 },
240
241 #[error("{message} while {context}")]
243 Sql {
244 message: String,
246 context: String,
248 },
249}
250
251pub trait DbContextExt<T> {
253 fn context(self, msg: impl Into<String>) -> Result<T, DatabaseError>;
254}
255
256impl<T> DbContextExt<T> for Result<T, sea_orm::DbErr> {
257 fn context(self, context: impl Into<String>) -> Result<T, DatabaseError> {
258 self.map_err(|err| match err.sql_err() {
259 Some(sea_orm::SqlErr::UniqueConstraintViolation(descr)) => DatabaseError::Conflict {
260 message: descr.to_string(),
261 context: context.into(),
262 },
263 Some(sea_orm::SqlErr::ForeignKeyConstraintViolation(descr)) => {
264 DatabaseError::Conflict {
265 message: descr.to_string(),
266 context: context.into(),
267 }
268 }
269 Some(other) => DatabaseError::Sql {
270 message: other.to_string(),
271 context: context.into(),
272 },
273 None => DatabaseError::Database {
274 source: err,
275 context: context.into(),
276 },
277 })
278 }
279}