Skip to content

Commit f2f2d30

Browse files
authored
refactor: Continue cleaning Errors (#466)
1 parent d4d787b commit f2f2d30

File tree

31 files changed

+336
-547
lines changed

31 files changed

+336
-547
lines changed

src/api/error.rs

Lines changed: 92 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -32,126 +32,81 @@ use crate::resource::error::ResourceProviderError;
3232
use crate::revoke::error::RevokeProviderError;
3333
use crate::token::error::TokenProviderError;
3434

35-
/// Keystone API operation errors
35+
/// Keystone API operation errors.
3636
#[derive(Debug, Error)]
3737
pub enum KeystoneApiError {
38-
#[error("conflict, resource already existing")]
39-
Conflict(String),
40-
41-
#[error("could not find {resource}: {identifier}")]
42-
NotFound {
43-
resource: String,
44-
identifier: String,
45-
},
38+
/// Selected authentication is forbidden.
39+
#[error("changing current authentication scope is forbidden")]
40+
AuthenticationRescopeForbidden,
4641

4742
#[error("Attempted to authenticate with an unsupported method.")]
4843
AuthMethodNotSupported,
4944

5045
#[error("{0}.")]
5146
BadRequest(String),
5247

53-
#[error("{}", .0.clone().unwrap_or("The request you have made requires authentication.".to_string()))]
54-
Unauthorized(Option<String>),
48+
/// Base64 decoding error.
49+
#[error(transparent)]
50+
Base64Decode(#[from] base64::DecodeError),
51+
52+
#[error("conflict, resource already existing")]
53+
Conflict(String),
54+
55+
#[error("domain id or name must be present")]
56+
DomainIdOrName,
5557

5658
#[error("You are not authorized to perform the requested action.")]
5759
Forbidden,
5860

59-
#[error("missing x-subject-token header")]
60-
SubjectTokenMissing,
61-
6261
#[error("invalid header header")]
6362
InvalidHeader,
6463

6564
#[error("invalid token")]
6665
InvalidToken,
6766

68-
#[error("error building token data: {}", source)]
69-
Token {
70-
#[from]
71-
source: TokenError,
72-
},
67+
#[error(transparent)]
68+
JsonExtractorRejection(#[from] JsonRejection),
7369

7470
#[error("internal server error: {0}")]
7571
InternalError(String),
7672

77-
#[error(transparent)]
78-
AssignmentError {
79-
//#[from]
80-
source: AssignmentProviderError,
81-
},
82-
83-
// #[error(transparent)]
84-
// AuthenticationInfo {
85-
// //#[from]
86-
// source: crate::auth::AuthenticationError,
87-
// },
88-
#[error(transparent)]
89-
CatalogError {
90-
#[from]
91-
source: CatalogProviderError,
73+
#[error("could not find {resource}: {identifier}")]
74+
NotFound {
75+
resource: String,
76+
identifier: String,
9277
},
9378

79+
/// Others.
9480
#[error(transparent)]
95-
IdentityError { source: IdentityProviderError },
81+
Other(#[from] eyre::Report),
9682

9783
#[error(transparent)]
9884
Policy {
9985
#[from]
10086
source: PolicyError,
10187
},
102-
103-
#[error(transparent)]
104-
ResourceError {
105-
#[from]
106-
source: ResourceProviderError,
107-
},
108-
109-
/// Revoke provider error.
110-
#[error(transparent)]
111-
RevokeProvider {
112-
/// The source of the error.
113-
#[from]
114-
source: RevokeProviderError,
115-
},
116-
117-
#[error(transparent)]
118-
TokenError { source: TokenProviderError },
119-
120-
#[error(transparent)]
121-
Uuid {
122-
#[from]
123-
source: uuid::Error,
124-
},
125-
126-
#[error(transparent)]
127-
Serde {
128-
#[from]
129-
source: serde_json::Error,
130-
},
131-
132-
/// Base64 decoding error.
133-
#[error(transparent)]
134-
Base64Decode(#[from] base64::DecodeError),
135-
136-
#[error("domain id or name must be present")]
137-
DomainIdOrName,
138-
13988
#[error("project id or name must be present")]
14089
ProjectIdOrName,
14190

14291
#[error("project domain must be present")]
14392
ProjectDomain,
14493

145-
#[error(transparent)]
146-
JsonExtractorRejection(#[from] JsonRejection),
147-
14894
/// Selected authentication is forbidden.
14995
#[error("selected authentication is forbidden")]
15096
SelectedAuthenticationForbidden,
15197

152-
/// Selected authentication is forbidden.
153-
#[error("changing current authentication scope is forbidden")]
154-
AuthenticationRescopeForbidden,
98+
/// (de)serialization error.
99+
#[error(transparent)]
100+
Serde {
101+
#[from]
102+
source: serde_json::Error,
103+
},
104+
105+
#[error("missing x-subject-token header")]
106+
SubjectTokenMissing,
107+
108+
#[error("{}", .0.clone().unwrap_or("The request you have made requires authentication.".to_string()))]
109+
Unauthorized(Option<String>),
155110

156111
/// Request validation error.
157112
#[error("request validation failed: {source}")]
@@ -160,10 +115,6 @@ pub enum KeystoneApiError {
160115
#[from]
161116
source: validator::ValidationErrors,
162117
},
163-
164-
/// Others.
165-
#[error(transparent)]
166-
Other(#[from] eyre::Report),
167118
}
168119

169120
impl IntoResponse for KeystoneApiError {
@@ -179,22 +130,10 @@ impl IntoResponse for KeystoneApiError {
179130
KeystoneApiError::Policy { .. } => StatusCode::FORBIDDEN,
180131
KeystoneApiError::SelectedAuthenticationForbidden
181132
| KeystoneApiError::AuthenticationRescopeForbidden => StatusCode::BAD_REQUEST,
182-
KeystoneApiError::InternalError(_)
183-
| KeystoneApiError::IdentityError { .. }
184-
| KeystoneApiError::ResourceError { .. }
185-
| KeystoneApiError::AssignmentError { .. }
186-
| KeystoneApiError::TokenError { .. }
187-
| KeystoneApiError::RevokeProvider { .. }
188-
| KeystoneApiError::Other(..) => StatusCode::INTERNAL_SERVER_ERROR,
189-
_ =>
190-
// KeystoneApiError::SubjectTokenMissing | KeystoneApiError::InvalidHeader |
191-
// KeystoneApiError::InvalidToken | KeystoneApiError::Token{..} |
192-
// KeystoneApiError::WebAuthN{..} | KeystoneApiError::Uuid {..} |
193-
// KeystoneApiError::Serde {..} | KeystoneApiError::DomainIdOrName |
194-
// KeystoneApiError::ProjectIdOrName | KeystoneApiError::ProjectDomain =>
195-
{
196-
StatusCode::BAD_REQUEST
133+
KeystoneApiError::InternalError(_) | KeystoneApiError::Other(..) => {
134+
StatusCode::INTERNAL_SERVER_ERROR
197135
}
136+
_ => StatusCode::BAD_REQUEST,
198137
};
199138

200139
(
@@ -205,86 +144,10 @@ impl IntoResponse for KeystoneApiError {
205144
}
206145
}
207146

208-
impl KeystoneApiError {
209-
pub fn identity(source: IdentityProviderError) -> Self {
210-
match source {
211-
IdentityProviderError::UserNotFound(x) => Self::NotFound {
212-
resource: "user".into(),
213-
identifier: x,
214-
},
215-
IdentityProviderError::GroupNotFound(x) => Self::NotFound {
216-
resource: "group".into(),
217-
identifier: x,
218-
},
219-
_ => source.into(),
220-
}
221-
}
222-
223-
pub fn resource(source: ResourceProviderError) -> Self {
224-
match source {
225-
ResourceProviderError::DomainNotFound(x) => Self::NotFound {
226-
resource: "domain".into(),
227-
identifier: x,
228-
},
229-
_ => source.into(),
230-
}
231-
}
232-
pub fn token(source: TokenProviderError) -> Self {
233-
match source {
234-
TokenProviderError::TokenRestrictionNotFound(x) => Self::NotFound {
235-
resource: "token restriction".into(),
236-
identifier: x,
237-
},
238-
_ => source.into(),
239-
}
240-
}
241-
}
242-
243-
#[derive(Debug, Error)]
244-
pub enum TokenError {
245-
#[error("error building token data: {}", source)]
246-
Builder {
247-
#[from]
248-
source: crate::api::v3::auth::token::types::TokenBuilderError,
249-
},
250-
251-
#[error("error building token data: {}", source)]
252-
Builder4 {
253-
#[from]
254-
source: crate::api::v4::auth::token::types::TokenBuilderError,
255-
},
256-
257-
#[error("error building token user data: {}", source)]
258-
UserBuilder {
259-
#[from]
260-
source: crate::api::v3::auth::token::types::UserBuilderError,
261-
},
262-
263-
#[error("error building token user data: {}", source)]
264-
UserBuilder4 {
265-
#[from]
266-
source: crate::api::v4::auth::token::types::UserBuilderError,
267-
},
268-
269-
#[error("error building token user data: {}", source)]
270-
ProjectBuilder {
271-
#[from]
272-
source: crate::api::types::ProjectBuilderError,
273-
},
274-
275-
/// Structures builder error.
276-
#[error(transparent)]
277-
StructBuilder {
278-
/// The source of the error.
279-
#[from]
280-
source: BuilderError,
281-
},
282-
}
283-
284147
impl From<AuthenticationError> for KeystoneApiError {
285148
fn from(value: AuthenticationError) -> Self {
286149
match value {
287-
AuthenticationError::AuthenticatedInfoBuilder { source } => {
150+
AuthenticationError::StructBuilder { source } => {
288151
KeystoneApiError::InternalError(source.to_string())
289152
}
290153
AuthenticationError::UserDisabled(user_id) => KeystoneApiError::Unauthorized(Some(
@@ -316,7 +179,25 @@ impl From<AssignmentProviderError> for KeystoneApiError {
316179
resource: "role".into(),
317180
identifier: x,
318181
},
319-
ref cfl @ AssignmentProviderError::Conflict(..) => Self::Conflict(cfl.to_string()),
182+
ref err @ AssignmentProviderError::Conflict(..) => Self::Conflict(err.to_string()),
183+
ref err @ AssignmentProviderError::Validation { .. } => {
184+
Self::BadRequest(err.to_string())
185+
}
186+
other => Self::InternalError(other.to_string()),
187+
}
188+
}
189+
}
190+
191+
impl From<BuilderError> for KeystoneApiError {
192+
fn from(value: crate::error::BuilderError) -> Self {
193+
Self::InternalError(value.to_string())
194+
}
195+
}
196+
197+
impl From<CatalogProviderError> for KeystoneApiError {
198+
fn from(value: CatalogProviderError) -> Self {
199+
match value {
200+
ref err @ CatalogProviderError::Conflict(..) => Self::Conflict(err.to_string()),
320201
other => Self::InternalError(other.to_string()),
321202
}
322203
}
@@ -326,22 +207,50 @@ impl From<IdentityProviderError> for KeystoneApiError {
326207
fn from(value: IdentityProviderError) -> Self {
327208
match value {
328209
IdentityProviderError::AuthenticationInfo { source } => source.into(),
329-
_ => Self::IdentityError { source: value },
210+
IdentityProviderError::UserNotFound(x) => Self::NotFound {
211+
resource: "user".into(),
212+
identifier: x,
213+
},
214+
IdentityProviderError::GroupNotFound(x) => Self::NotFound {
215+
resource: "group".into(),
216+
identifier: x,
217+
},
218+
other => Self::InternalError(other.to_string()),
330219
}
331220
}
332221
}
333222

334-
impl From<TokenProviderError> for KeystoneApiError {
335-
fn from(value: TokenProviderError) -> Self {
223+
impl From<ResourceProviderError> for KeystoneApiError {
224+
fn from(value: ResourceProviderError) -> Self {
336225
match value {
337-
TokenProviderError::AuthenticationInfo { source } => source.into(),
338-
_ => Self::TokenError { source: value },
226+
ref err @ ResourceProviderError::Conflict(..) => Self::BadRequest(err.to_string()),
227+
ResourceProviderError::DomainNotFound(x) => Self::NotFound {
228+
resource: "domain".into(),
229+
identifier: x,
230+
},
231+
other => Self::InternalError(other.to_string()),
339232
}
340233
}
341234
}
342235

343-
impl From<crate::error::BuilderError> for KeystoneApiError {
344-
fn from(value: crate::error::BuilderError) -> Self {
345-
Self::InternalError(value.to_string())
236+
impl From<RevokeProviderError> for KeystoneApiError {
237+
fn from(value: RevokeProviderError) -> Self {
238+
match value {
239+
ref err @ RevokeProviderError::Conflict(..) => Self::BadRequest(err.to_string()),
240+
other => Self::InternalError(other.to_string()),
241+
}
242+
}
243+
}
244+
245+
impl From<TokenProviderError> for KeystoneApiError {
246+
fn from(value: TokenProviderError) -> Self {
247+
match value {
248+
TokenProviderError::AuthenticationInfo(source) => source.into(),
249+
TokenProviderError::TokenRestrictionNotFound(x) => Self::NotFound {
250+
resource: "token restriction".into(),
251+
identifier: x,
252+
},
253+
other => Self::InternalError(other.to_string()),
254+
}
346255
}
347256
}

0 commit comments

Comments
 (0)