Restructured client error impls.
This commit is contained in:
+4
-166
@@ -1,27 +1,16 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
domain::DomainError,
|
||||
repository::RepositoryError,
|
||||
routes::extractors::{AuthError, AuthHeaderError, BearerError, ExtractorError},
|
||||
};
|
||||
use crate::{domain::DomainError, repository::RepositoryError, routes::extractors::ExtractorError};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
#[error("app id is already in use")]
|
||||
AppIdAlreadyExists,
|
||||
|
||||
#[error("invalid client credentials")]
|
||||
InvalidClientCredentials,
|
||||
|
||||
#[error("invalid refresh token")]
|
||||
InvalidRefreshToken,
|
||||
|
||||
#[error("app id is already in use")]
|
||||
AppIdAlreadyExists,
|
||||
|
||||
#[error("not found")]
|
||||
NotFound,
|
||||
|
||||
@@ -40,154 +29,3 @@ pub enum AppError {
|
||||
#[error(transparent)]
|
||||
Internal(#[from] anyhow::Error),
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, message) = self.client_response();
|
||||
|
||||
if status.is_server_error() {
|
||||
tracing::error!(error = ?self, %status, "request failed");
|
||||
} else {
|
||||
tracing::debug!(error = ?self, %status, "request rejected");
|
||||
}
|
||||
|
||||
error_response(status, message)
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for AppError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
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::Domain(error) => error.client_response(),
|
||||
Self::Extractor(error) => error.client_response(),
|
||||
Self::Repository(error) => error.client_response(),
|
||||
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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for AuthError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::UnsupportedScheme => (StatusCode::UNAUTHORIZED, "Unsupported scheme".into()),
|
||||
Self::Bearer(error) => error.client_response(),
|
||||
Self::Header(error) => error.client_response(),
|
||||
Self::Repository(error) => error.client_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
Self::Missing => "Missing authorization header",
|
||||
Self::NotAscii(_) => "Invalid authorization header encoding",
|
||||
Self::MalformedValue => "Malformed authorization header",
|
||||
Self::EmptyScheme => "Empty authorization scheme",
|
||||
Self::EmptyToken => "Empty authorization token",
|
||||
};
|
||||
|
||||
(StatusCode::UNAUTHORIZED, message.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for DomainError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientError for RepositoryError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>) {
|
||||
match self {
|
||||
Self::Database(_) | Self::Decode(_) => internal_error(),
|
||||
Self::QueryConversion(error) => (StatusCode::BAD_REQUEST, error.to_string().into()),
|
||||
Self::ReferenceViolation { relation, kind, .. } => {
|
||||
use crate::repository::{
|
||||
ReferenceRelation as Relation, ReferenceViolationKind as Kind,
|
||||
};
|
||||
match (relation, kind) {
|
||||
(Relation::Account, Kind::Missing) => (
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"Account cannot be found".into(),
|
||||
),
|
||||
(Relation::RecurringTransaction, Kind::StillReferenced) => (
|
||||
StatusCode::CONFLICT,
|
||||
"Still referenced by one or more recurring transactions".into(),
|
||||
),
|
||||
(Relation::Transaction, Kind::StillReferenced) => (
|
||||
StatusCode::CONFLICT,
|
||||
"Still referenced by one or more transactions".into(),
|
||||
),
|
||||
(Relation::User, Kind::Missing) => {
|
||||
(StatusCode::NOT_FOUND, "User cannot be found".into())
|
||||
}
|
||||
(Relation::UserApp, Kind::Missing) => {
|
||||
(StatusCode::NOT_FOUND, "User app cannot be found".into())
|
||||
}
|
||||
_ => internal_error(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait ClientError {
|
||||
fn client_response(&self) -> (StatusCode, Cow<'_, str>);
|
||||
}
|
||||
|
||||
fn internal_error() -> (StatusCode, Cow<'static, str>) {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"Internal server error".into(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct ErrorResponse {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
fn error_response(status_code: StatusCode, message: impl Into<String>) -> Response {
|
||||
(
|
||||
status_code,
|
||||
axum::Json(ErrorResponse {
|
||||
message: message.into(),
|
||||
}),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user