Continuing with error refactoring.
This commit is contained in:
+56
-34
@@ -8,34 +8,40 @@ use axum::{
|
||||
use crate::{
|
||||
domain::DomainError,
|
||||
repository::RepositoryError,
|
||||
routes::extractors::auth::{AuthError, AuthHeaderError, BearerError},
|
||||
routes::extractors::{
|
||||
ExtractorError,
|
||||
auth::{AuthError, AuthHeaderError, BearerError},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
#[error("bad request")]
|
||||
BadRequest(String),
|
||||
#[error("invalid client credentials")]
|
||||
InvalidClientCredentials,
|
||||
|
||||
#[error("conflict")]
|
||||
Conflict(String),
|
||||
#[error("invalid refresh token")]
|
||||
InvalidRefreshToken,
|
||||
|
||||
#[error("app id is already in use")]
|
||||
AppIdAlreadyExists,
|
||||
|
||||
#[error("not found")]
|
||||
NotFound,
|
||||
|
||||
#[error("unauthorized")]
|
||||
Unauthorized(String),
|
||||
#[error("unknown")]
|
||||
Unknown,
|
||||
|
||||
#[error("authorization error")]
|
||||
Authorization(#[from] AuthError),
|
||||
|
||||
#[error("domain error")]
|
||||
#[error(transparent)]
|
||||
Domain(#[from] DomainError),
|
||||
|
||||
#[error("repository error")]
|
||||
#[error(transparent)]
|
||||
Extractor(#[from] ExtractorError),
|
||||
|
||||
#[error(transparent)]
|
||||
Repository(#[from] RepositoryError),
|
||||
|
||||
#[error("internal server error")]
|
||||
Internal,
|
||||
#[error(transparent)]
|
||||
Internal(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
@@ -44,8 +50,6 @@ impl IntoResponse for AppError {
|
||||
|
||||
if status.is_server_error() {
|
||||
tracing::error!(error = ?self, %status, "request failed");
|
||||
} else if matches!(self, Self::Conflict(_)) {
|
||||
tracing::warn!(error = ?self, %status, "request rejected");
|
||||
} else {
|
||||
tracing::debug!(error = ?self, %status, "request rejected");
|
||||
}
|
||||
@@ -57,14 +61,29 @@ impl IntoResponse for AppError {
|
||||
impl ClientError for AppError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::BadRequest(message) => (StatusCode::BAD_REQUEST, message.as_str().into()),
|
||||
Self::Unauthorized(message) => (StatusCode::UNAUTHORIZED, message.as_str().into()),
|
||||
Self::InvalidClientCredentials => (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"Invalid client credentials".into(),
|
||||
),
|
||||
Self::InvalidRefreshToken => (StatusCode::UNAUTHORIZED, "Invalid refresh token".into()),
|
||||
Self::AppIdAlreadyExists => (StatusCode::CONFLICT, "App ID already exists".into()),
|
||||
Self::NotFound => (StatusCode::NOT_FOUND, "Not found".into()),
|
||||
Self::Conflict(message) => (StatusCode::CONFLICT, message.as_str().into()),
|
||||
Self::Authorization(error) => error.client_response(),
|
||||
Self::Domain(error) => error.client_response(),
|
||||
Self::Extractor(error) => error.client_response(),
|
||||
Self::Repository(error) => error.client_response(),
|
||||
Self::Internal => internal_error(),
|
||||
Self::Internal(_) | Self::Unknown => internal_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for ExtractorError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::Authorization(error) => error.client_response(),
|
||||
Self::Form(_) => (StatusCode::BAD_REQUEST, "Form error".into()),
|
||||
Self::Json(_) => (StatusCode::BAD_REQUEST, "Json error".into()),
|
||||
Self::Path(_) => (StatusCode::BAD_REQUEST, "Path error".into()),
|
||||
Self::Query(_) => (StatusCode::BAD_REQUEST, "Query error".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,6 +99,15 @@ impl ClientError for AuthError {
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for BearerError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::InvalidCredentials => (StatusCode::UNAUTHORIZED, "Invalid credentials".into()),
|
||||
Self::Repository(error) => error.client_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for AuthHeaderError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
let message = match self {
|
||||
@@ -94,21 +122,15 @@ impl ClientError for AuthHeaderError {
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for BearerError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::InvalidCredentials => (StatusCode::UNAUTHORIZED, "Invalid credentials".into()),
|
||||
Self::Repository(error) => error.client_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for DomainError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::Amount(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string().into()),
|
||||
Self::Schedule(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string().into()),
|
||||
}
|
||||
let message: String = match self {
|
||||
Self::Amount(error) => error.to_string(),
|
||||
Self::Schedule(error) => error.to_string(),
|
||||
Self::UserAppSecret(error) => error.to_string(),
|
||||
};
|
||||
|
||||
(StatusCode::UNPROCESSABLE_ENTITY, message.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user