79 lines
2.1 KiB
Rust
79 lines
2.1 KiB
Rust
pub mod accounts;
|
|
pub mod dtos;
|
|
mod error_codes;
|
|
pub mod recurring_transactions;
|
|
pub mod tokens;
|
|
pub mod transactions;
|
|
pub mod user_app_secrets;
|
|
pub mod user_apps;
|
|
pub mod users;
|
|
|
|
use error_codes::PG_FOREIGN_KEY_VIOLATION;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum DatabaseError {
|
|
#[error("foreign key violation ({constraint}): {source}")]
|
|
ForeignKeyViolation {
|
|
constraint: Constraint,
|
|
#[source]
|
|
source: sqlx::Error,
|
|
},
|
|
|
|
#[error("internal error: {0}")]
|
|
Internal(#[source] sqlx::Error),
|
|
}
|
|
|
|
impl From<sqlx::Error> for DatabaseError {
|
|
fn from(err: sqlx::Error) -> Self {
|
|
match err {
|
|
sqlx::Error::Database(db_err)
|
|
if db_err.code().as_deref() == Some(PG_FOREIGN_KEY_VIOLATION) =>
|
|
{
|
|
let constraint = db_err
|
|
.constraint()
|
|
.map(Constraint::from)
|
|
.unwrap_or(Constraint::Unnamed);
|
|
|
|
DatabaseError::ForeignKeyViolation {
|
|
constraint,
|
|
source: sqlx::Error::Database(db_err),
|
|
}
|
|
}
|
|
_ => DatabaseError::Internal(err),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
|
pub enum Constraint {
|
|
#[error("user app user constraint")]
|
|
UserAppUser,
|
|
|
|
#[error("user app secret app constraint")]
|
|
UserAppSecretApp,
|
|
|
|
#[error("transaction account constraint")]
|
|
TransactionAccount,
|
|
|
|
#[error("recurring transaction account constraint")]
|
|
RecurringTransactionAccount,
|
|
|
|
#[error("unknown constraint ({0:?})")]
|
|
Unknown(String),
|
|
|
|
#[error("unnamed constraint")]
|
|
Unnamed,
|
|
}
|
|
|
|
impl From<&str> for Constraint {
|
|
fn from(constraint: &str) -> Self {
|
|
match constraint {
|
|
"fk_user_apps_user" => Constraint::UserAppUser,
|
|
"fk_user_app_secrets_app" => Constraint::UserAppSecretApp,
|
|
"fk_transactions_account" => Constraint::TransactionAccount,
|
|
"fk_recurring_transactions_account" => Constraint::RecurringTransactionAccount,
|
|
_ => Constraint::Unknown(constraint.to_string()),
|
|
}
|
|
}
|
|
}
|