diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 901a0ff49..843edd48c 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -177,6 +177,11 @@ pub trait DatabaseError: Display + Debug + Send + Sync { /// The primary, human-readable error message. fn message(&self) -> &str; + /// The (SQLSTATE) code for the error. + fn code(&self) -> Option<&str> { + None + } + fn details(&self) -> Option<&str> { None } diff --git a/sqlx-core/src/mysql/error.rs b/sqlx-core/src/mysql/error.rs index a90649a6e..3d9aa44fa 100644 --- a/sqlx-core/src/mysql/error.rs +++ b/sqlx-core/src/mysql/error.rs @@ -16,4 +16,8 @@ impl DatabaseError for MySqlError { fn message(&self) -> &str { &*self.0.error_message } + + fn code(&self) -> Option<&str> { + self.0.sql_state.as_deref() + } } diff --git a/sqlx-core/src/postgres/error.rs b/sqlx-core/src/postgres/error.rs index e6f024edb..546ead3ea 100644 --- a/sqlx-core/src/postgres/error.rs +++ b/sqlx-core/src/postgres/error.rs @@ -11,6 +11,10 @@ impl DatabaseError for PgError { &self.0.message } + fn code(&self) -> Option<&str> { + Some(&self.0.code) + } + fn details(&self) -> Option<&str> { self.0.detail.as_ref().map(|s| &**s) } diff --git a/sqlx-core/src/sqlite/error.rs b/sqlx-core/src/sqlite/error.rs index 13b83fa0f..7cfcd2b97 100644 --- a/sqlx-core/src/sqlite/error.rs +++ b/sqlx-core/src/sqlite/error.rs @@ -7,8 +7,7 @@ use std::os::raw::c_int; #[derive(Debug)] pub struct SqliteError { - #[allow(dead_code)] - code: c_int, + code: String, message: String, } @@ -29,7 +28,7 @@ impl SqliteError { }; Self { - code, + code: code.to_string(), message: message.to_owned(), } } @@ -45,4 +44,8 @@ impl DatabaseError for SqliteError { fn message(&self) -> &str { &self.message } + + fn code(&self) -> Option<&str> { + &self.code + } }