Communicate known foreign key violations to the user.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
DecodeError, QueryConversionError, RepositoryError,
|
||||
DecodeError, QueryConversionError, ReferenceRelation, ReferenceViolationKind, RepositoryError,
|
||||
dtos::{Delete, Upsert},
|
||||
encoding::{
|
||||
decode_direction, decode_recurrence_unit, decode_weekend_adjustment, encode_direction,
|
||||
@@ -9,7 +9,9 @@ use super::{
|
||||
},
|
||||
};
|
||||
use crate::{
|
||||
database::{dtos::RecurringTransactionRow, recurring_transactions as db},
|
||||
database::{
|
||||
Constraint, DatabaseError, dtos::RecurringTransactionRow, recurring_transactions as db,
|
||||
},
|
||||
domain::{Cents, Recurrence, RecurrenceInterval, RecurringTransaction, Schedule},
|
||||
};
|
||||
|
||||
@@ -81,12 +83,36 @@ where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
{
|
||||
let transaction = db::upsert(executor, encode(transaction))
|
||||
.await?
|
||||
.await
|
||||
.map_err(classify_upsert_error)?
|
||||
.try_map(decode)?;
|
||||
|
||||
Ok(transaction.into())
|
||||
}
|
||||
|
||||
fn classify_upsert_error(error: DatabaseError) -> RepositoryError {
|
||||
let relation = match &error {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionSourceAccount,
|
||||
..
|
||||
} => Some(ReferenceRelation::SourceAccount),
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint: Constraint::RecurringTransactionDestinationAccount,
|
||||
..
|
||||
} => Some(ReferenceRelation::DestinationAccount),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match relation {
|
||||
Some(relation) => RepositoryError::ReferenceViolation {
|
||||
relation,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
source: error,
|
||||
},
|
||||
None => RepositoryError::Database(error),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete<'e, E>(executor: E, id: Uuid) -> Result<Delete, RepositoryError>
|
||||
where
|
||||
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
|
||||
@@ -144,6 +170,51 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::domain::{Direction, RecurrenceUnit, ScheduleError, WeekendAdjustment};
|
||||
|
||||
fn foreign_key_violation(constraint: Constraint) -> DatabaseError {
|
||||
DatabaseError::ForeignKeyViolation {
|
||||
constraint,
|
||||
source: sqlx::Error::Protocol("test database cause".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_upsert_foreign_key_constraints() {
|
||||
let source = classify_upsert_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionSourceAccount,
|
||||
));
|
||||
assert!(matches!(
|
||||
&source,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::SourceAccount,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
));
|
||||
|
||||
let database_error = std::error::Error::source(&source).unwrap();
|
||||
let sqlx_error = std::error::Error::source(database_error).unwrap();
|
||||
assert!(sqlx_error.to_string().contains("test database cause"));
|
||||
|
||||
let destination = classify_upsert_error(foreign_key_violation(
|
||||
Constraint::RecurringTransactionDestinationAccount,
|
||||
));
|
||||
assert!(matches!(
|
||||
&destination,
|
||||
RepositoryError::ReferenceViolation {
|
||||
relation: ReferenceRelation::DestinationAccount,
|
||||
kind: ReferenceViolationKind::Missing,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_unknown_upsert_foreign_keys_generic() {
|
||||
let error = classify_upsert_error(foreign_key_violation(Constraint::Unknown(None)));
|
||||
|
||||
assert!(matches!(error, RepositoryError::Database(_)));
|
||||
}
|
||||
|
||||
fn row() -> RecurringTransactionRow {
|
||||
RecurringTransactionRow {
|
||||
id: Uuid::new_v4(),
|
||||
|
||||
Reference in New Issue
Block a user