Struggling to clean up error handling.

This commit is contained in:
2026-10-10 17:18:36 +02:00
parent 5f85713892
commit 94b2f23a59
4 changed files with 64 additions and 48 deletions
+28 -7
View File
@@ -6,6 +6,7 @@ use axum::{
use crate::{
domain::DomainError,
repository::{ReferenceRelation, ReferenceViolationKind, RepositoryError},
routes::extractors::auth::AuthError,
};
#[derive(Debug, thiserror::Error)]
@@ -22,6 +23,9 @@ pub enum AppError {
#[error("unauthorized")]
Unauthorized(String),
#[error("authorization error")]
Authorization(#[from] AuthError),
#[error("domain error")]
Domain(#[from] DomainError),
@@ -51,6 +55,21 @@ impl IntoResponse for AppError {
tracing::debug!(?message, "unauthorized");
return error_response(StatusCode::UNAUTHORIZED, message);
}
Self::Authorization(AuthError::UnsupportedScheme) => {
tracing::debug!("authorization error: unsupported scheme");
return error_response(StatusCode::UNAUTHORIZED, "Unsupported scheme");
}
Self::Authorization(AuthError::Header(error)) => {
tracing::debug!(%error, "authorization header error");
return error_response(StatusCode::UNAUTHORIZED, error.client_error_message());
}
Self::Authorization(AuthError::Bearer(error)) => {
tracing::debug!(%error, "bearer token error");
return error_response(StatusCode::UNAUTHORIZED, error.client_error_message());
}
Self::Authorization(AuthError::Repository(error)) => {
tracing::debug!(%error, "repository error");
}
Self::Domain(error) => {
tracing::debug!(%error, "domain error");
return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string());
@@ -136,14 +155,16 @@ struct ErrorResponse {
pub message: String,
}
impl ErrorResponse {
pub fn new(message: impl Into<String>) -> Self {
Self {
fn error_response(status_code: StatusCode, message: impl Into<String>) -> Response {
(
status_code,
axum::Json(ErrorResponse {
message: message.into(),
}
}
}),
)
.into_response()
}
fn error_response(status_code: StatusCode, message: impl Into<String>) -> Response {
(status_code, axum::Json(ErrorResponse::new(message))).into_response()
pub trait ClientError {
fn client_error_message(&self) -> &'static str;
}