-
-
Notifications
You must be signed in to change notification settings - Fork 504
Description
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),
}