Skip to content

simplify specific db errors matching #1194

@Samiisd

Description

@Samiisd

Motivation

Currently, matching DB specific errors is highly verbose, and I believe some common errors could benefit from being simplified. Current solution is the following: #1152

E.g.

match user::Entity::insert_many(models).exec(&state.db).await {
    Err(DbErr::Query(RuntimeErr::SqlxError(sqlx::Error::Database(e))))
        if e.code().unwrap().eq("23505") =>
    {
        // 23505 is postgresql error code for violation on column unique constraint
        Ok()
    }
    Err(e) => Err(e),
    Ok(_) => Ok()
}

Proposed Solutions

I don't know every use cases and common errors, but I believe this ORM would benefit from simplifying error handling.
Another generic enum error could be created + custom DB error mapping logic to this newly created enum.

E.g.

enum DbErr {
     ForeignKeyViolation { column: String, key: int },
     NotNullViolation { column: String },
     UniqueViolation { column: String },
     ...
}

Additional Information

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions