use axum::{ http::StatusCode, response::{IntoResponse, Response}, }; use crate::{ domain::DomainError, repository::{ReferenceRelation, ReferenceViolationKind, RepositoryError}, }; #[derive(Debug, thiserror::Error)] pub enum AppError { #[error("bad request")] BadRequest(String), #[error("conflict")] Conflict(String), #[error("not found")] NotFound, #[error("unauthorized")] Unauthorized(String), #[error("domain error")] Domain(#[from] DomainError), #[error("repository error")] Repository(#[from] RepositoryError), #[error("internal server error")] Internal, } impl IntoResponse for AppError { fn into_response(self) -> Response { match self { Self::BadRequest(error) => { tracing::debug!(%error, "bad request"); return error_response(StatusCode::BAD_REQUEST, error); } Self::Conflict(error) => { tracing::warn!(%error, "conflict"); return error_response(StatusCode::CONFLICT, error); } Self::NotFound => { tracing::debug!("not found"); return error_response(StatusCode::NOT_FOUND, "Not found"); } Self::Unauthorized(message) => { tracing::debug!(?message, "unauthorized"); return error_response(StatusCode::UNAUTHORIZED, message); } Self::Domain(error) => { tracing::debug!(%error, "domain error"); return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); } Self::Repository(RepositoryError::Database(error)) => { tracing::error!(%error, "database query failed"); } Self::Repository(RepositoryError::Decode(error)) => { tracing::error!(%error, "invalid stored data"); } Self::Repository(RepositoryError::ReferenceViolation { relation: ReferenceRelation::User, kind: ReferenceViolationKind::Missing, .. }) => { tracing::debug!("user not found (reference violation)"); return error_response(StatusCode::NOT_FOUND, "User cannot be found"); } Self::Repository(RepositoryError::ReferenceViolation { relation: ReferenceRelation::UserApp, kind: ReferenceViolationKind::Missing, .. }) => { tracing::debug!("user app not found (reference violation)"); return error_response(StatusCode::NOT_FOUND, "User app cannot be found"); } Self::Repository(RepositoryError::ReferenceViolation { relation: ReferenceRelation::Account, kind: ReferenceViolationKind::Missing, .. }) => { tracing::debug!("account not found (reference violation)"); return error_response(StatusCode::UNPROCESSABLE_ENTITY, "Account cannot be found"); } Self::Repository(RepositoryError::ReferenceViolation { relation: ReferenceRelation::Transaction, kind: ReferenceViolationKind::StillReferenced, .. }) => { tracing::debug!( "still referenced by one or more transactions (reference violation)" ); return error_response( StatusCode::CONFLICT, "Still referenced by one or more transactions", ); } Self::Repository(RepositoryError::ReferenceViolation { relation: ReferenceRelation::RecurringTransaction, kind: ReferenceViolationKind::StillReferenced, .. }) => { tracing::debug!( "still referenced by one or more recurring transactions (reference violation)" ); return error_response( StatusCode::CONFLICT, "Still referenced by one or more recurring transactions", ); } Self::Repository(RepositoryError::ReferenceViolation { relation, kind, source, }) => { tracing::error!(%relation, %kind, error = ?source, "unhandled reference violation"); } Self::Repository(RepositoryError::QueryConversion(error)) => { tracing::debug!(%error, "query conversion error"); return error_response(StatusCode::BAD_REQUEST, error.to_string()); } Self::Internal => { tracing::error!("internal server error"); } } error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error") } } #[derive(serde::Serialize)] struct ErrorResponse { pub message: String, } impl ErrorResponse { pub fn new(message: impl Into) -> Self { Self { message: message.into(), } } } fn error_response(status_code: StatusCode, message: impl Into) -> Response { (status_code, axum::Json(ErrorResponse::new(message))).into_response() }