Add DatabaseError::code

This commit is contained in:
Ryan Leckey 2020-03-16 18:29:41 -07:00
parent 5cb0d9d9cc
commit 1d0100b35d
4 changed files with 19 additions and 3 deletions

View File

@ -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
}

View File

@ -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()
}
}

View File

@ -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)
}

View File

@ -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
}
}