From 48d58b979f918a91cec685e71312023ab77091e5 Mon Sep 17 00:00:00 2001 From: Luiz Carvalho Date: Fri, 14 Jul 2023 10:43:02 -0300 Subject: [PATCH] feat(sqlx-core): add table function to database error --- sqlx-core/src/error.rs | 8 ++++++++ sqlx-postgres/src/error.rs | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index bdca7e8abc..11d79ab6cf 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -212,6 +212,14 @@ pub trait DatabaseError: 'static + Send + Sync + StdError { None } + /// Returns the name of the table that was affected by the error, if applicable. + /// + /// ### Note + /// Currently only populated by the Postgres driver. + fn table(&self) -> Option<&str> { + None + } + /// Returns the kind of the error, if supported. /// /// ### Note diff --git a/sqlx-postgres/src/error.rs b/sqlx-postgres/src/error.rs index 8de58d5522..b9df865736 100644 --- a/sqlx-postgres/src/error.rs +++ b/sqlx-postgres/src/error.rs @@ -204,6 +204,10 @@ impl DatabaseError for PgDatabaseError { self.constraint() } + fn table(&self) -> Option<&str> { + self.table() + } + fn kind(&self) -> ErrorKind { match self.code() { error_codes::UNIQUE_VIOLATION => ErrorKind::UniqueViolation,