Error codes file.
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
pub mod dtos;
|
pub mod dtos;
|
||||||
|
mod error_codes;
|
||||||
pub mod recurring_transactions;
|
pub mod recurring_transactions;
|
||||||
pub mod transactions;
|
pub mod transactions;
|
||||||
|
|
||||||
|
use error_codes::PG_FOREIGN_KEY_VIOLATION;
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum DatabaseError {
|
pub enum DatabaseError {
|
||||||
#[error("foreign key violation ({constraint}): {source}")]
|
#[error("foreign key violation ({constraint}): {source}")]
|
||||||
@@ -19,7 +22,9 @@ pub enum DatabaseError {
|
|||||||
impl From<sqlx::Error> for DatabaseError {
|
impl From<sqlx::Error> for DatabaseError {
|
||||||
fn from(err: sqlx::Error) -> Self {
|
fn from(err: sqlx::Error) -> Self {
|
||||||
match err {
|
match err {
|
||||||
sqlx::Error::Database(db_err) if db_err.code().as_deref() == Some("23503") => {
|
sqlx::Error::Database(db_err)
|
||||||
|
if db_err.code().as_deref() == Some(PG_FOREIGN_KEY_VIOLATION) =>
|
||||||
|
{
|
||||||
let constraint = db_err
|
let constraint = db_err
|
||||||
.constraint()
|
.constraint()
|
||||||
.map(Constraint::from)
|
.map(Constraint::from)
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
pub const PG_FOREIGN_KEY_VIOLATION: &str = "23503";
|
||||||
Reference in New Issue
Block a user