Communicate known foreign key violations to the user.
This commit is contained in:
+41
-4
@@ -3,7 +3,10 @@ use axum::{
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
use crate::{database::DatabaseError, domain::DomainError, repository::RepositoryError};
|
||||
use crate::{
|
||||
domain::DomainError,
|
||||
repository::{ReferenceRelation, ReferenceViolationKind, RepositoryError},
|
||||
};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum AppError {
|
||||
@@ -32,15 +35,49 @@ impl IntoResponse for AppError {
|
||||
Self::Domain(error) => {
|
||||
return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string());
|
||||
}
|
||||
Self::Repository(RepositoryError::Database(DatabaseError::ForeignKeyViolation)) => {
|
||||
return error_response(StatusCode::UNPROCESSABLE_ENTITY, "Reference violation");
|
||||
}
|
||||
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::SourceAccount,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}) => {
|
||||
return error_response(
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"Source account cannot be found",
|
||||
);
|
||||
}
|
||||
Self::Repository(RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::DestinationAccount,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}) => {
|
||||
return error_response(
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"Destination account cannot be found",
|
||||
);
|
||||
}
|
||||
Self::Repository(RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::RecurringTransaction,
|
||||
kind: ReferenceViolationKind::StillReferenced,
|
||||
..
|
||||
}) => {
|
||||
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)) => {
|
||||
return error_response(StatusCode::BAD_REQUEST, error.to_string());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user