Skip to content

How can I handle DbErrors from a failed tokio_postgres query? #732

@blazzy

Description

@blazzy

I'm interested in grabbing the constraint property from a DbError. The higher level goal is to do optimistic inserts/updates and be notified of which specific constraint was violated if any.

It was challenging for me to get to this data starting with the tokio_postgres::Error returned from query():

Err(err) => {
    let source = err.into_source();
    let constraint = source
        .as_ref()
        .and_then(|e| e.downcast_ref::<tokio_postgres::error::DbError>())
        .and_then(|e| e.constraint());

     println!("{}", constraint.unwrap());

The complexity here makes me think I might be doing something wrong.

Another challenge presented, is that the original error is consumed by into_source. In the event, that there isn't a DbError with a constraint, I want to simply return the original error.

It's not clear to me if this is a feature request, or if I'm doing something stupid. I'm far too new to Rust to have constructive opinions, but I was expecting to be able to do something like this:

let constraint = match err {
   DbError(err) => err.constraint(),
   _ => return Err(err),
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions