Refactor SqlxError;
This commit is contained in:
parent
348e841139
commit
0ce0f49551
@ -116,7 +116,9 @@ impl ConnectionTrait for DatabaseConnection {
|
||||
DatabaseConnection::SqlxSqlitePoolConnection(conn) => conn.execute(stmt).await,
|
||||
#[cfg(feature = "mock")]
|
||||
DatabaseConnection::MockDatabaseConnection(conn) => conn.execute(stmt),
|
||||
DatabaseConnection::Disconnected => Err(DbErr::Conn("Disconnected".to_owned())),
|
||||
DatabaseConnection::Disconnected => {
|
||||
Err(DbErr::Conn(RuntimeErr::Internal("Disconnected".to_owned())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,7 +134,9 @@ impl ConnectionTrait for DatabaseConnection {
|
||||
DatabaseConnection::SqlxSqlitePoolConnection(conn) => conn.query_one(stmt).await,
|
||||
#[cfg(feature = "mock")]
|
||||
DatabaseConnection::MockDatabaseConnection(conn) => conn.query_one(stmt),
|
||||
DatabaseConnection::Disconnected => Err(DbErr::Conn("Disconnected".to_owned())),
|
||||
DatabaseConnection::Disconnected => {
|
||||
Err(DbErr::Conn(RuntimeErr::Internal("Disconnected".to_owned())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,7 +152,9 @@ impl ConnectionTrait for DatabaseConnection {
|
||||
DatabaseConnection::SqlxSqlitePoolConnection(conn) => conn.query_all(stmt).await,
|
||||
#[cfg(feature = "mock")]
|
||||
DatabaseConnection::MockDatabaseConnection(conn) => conn.query_all(stmt),
|
||||
DatabaseConnection::Disconnected => Err(DbErr::Conn("Disconnected".to_owned())),
|
||||
DatabaseConnection::Disconnected => {
|
||||
Err(DbErr::Conn(RuntimeErr::Internal("Disconnected".to_owned())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@ pub use stream::*;
|
||||
use tracing::instrument;
|
||||
pub use transaction::*;
|
||||
|
||||
use crate::DbErr;
|
||||
use crate::{DbErr, RuntimeErr};
|
||||
|
||||
/// Defines a database
|
||||
#[derive(Debug, Default)]
|
||||
@ -73,10 +73,10 @@ impl Database {
|
||||
if crate::MockDatabaseConnector::accepts(&opt.url) {
|
||||
return crate::MockDatabaseConnector::connect(&opt.url).await;
|
||||
}
|
||||
Err(DbErr::Conn(format!(
|
||||
Err(DbErr::Conn(RuntimeErr::Internal(format!(
|
||||
"The connection string '{}' has no supporting driver.",
|
||||
opt.url
|
||||
)))
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,16 +1,16 @@
|
||||
use crate::DbErr;
|
||||
use crate::{DbErr, RuntimeErr};
|
||||
|
||||
/// Converts an [sqlx::error] execution error to a [DbErr]
|
||||
pub fn sqlx_error_to_exec_err(err: sqlx::Error) -> DbErr {
|
||||
DbErr::ExecSqlX(err)
|
||||
DbErr::Exec(RuntimeErr::SqlxError(err))
|
||||
}
|
||||
|
||||
/// Converts an [sqlx::error] query error to a [DbErr]
|
||||
pub fn sqlx_error_to_query_err(err: sqlx::Error) -> DbErr {
|
||||
DbErr::QuerySqlX(err)
|
||||
DbErr::Query(RuntimeErr::SqlxError(err))
|
||||
}
|
||||
|
||||
/// Converts an [sqlx::error] connection error to a [DbErr]
|
||||
pub fn sqlx_error_to_conn_err(err: sqlx::Error) -> DbErr {
|
||||
DbErr::ConnSqlX(err)
|
||||
DbErr::Conn(RuntimeErr::SqlxError(err))
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ impl SqlxMySqlConnector {
|
||||
let mut opt = options
|
||||
.url
|
||||
.parse::<MySqlConnectOptions>()
|
||||
.map_err(|e| DbErr::ConnSqlX(e))?;
|
||||
.map_err(sqlx_error_to_conn_err)?;
|
||||
use sqlx::ConnectOptions;
|
||||
if !options.sqlx_logging {
|
||||
opt.disable_statement_logging();
|
||||
|
@ -45,7 +45,7 @@ impl SqlxPostgresConnector {
|
||||
let mut opt = options
|
||||
.url
|
||||
.parse::<PgConnectOptions>()
|
||||
.map_err(|e| DbErr::ConnSqlX(e))?;
|
||||
.map_err(sqlx_error_to_conn_err)?;
|
||||
use sqlx::ConnectOptions;
|
||||
if !options.sqlx_logging {
|
||||
opt.disable_statement_logging();
|
||||
|
@ -46,7 +46,7 @@ impl SqlxSqliteConnector {
|
||||
let mut opt = options
|
||||
.url
|
||||
.parse::<SqliteConnectOptions>()
|
||||
.map_err(|e| DbErr::ConnSqlX(e))?;
|
||||
.map_err(sqlx_error_to_conn_err)?;
|
||||
if options.sqlcipher_key.is_some() {
|
||||
opt = opt.pragma("key", options.sqlcipher_key.clone().unwrap());
|
||||
}
|
||||
|
27
src/error.rs
27
src/error.rs
@ -30,22 +30,13 @@ pub enum DbErr {
|
||||
UpdateCouldNotGetPrimaryKey,
|
||||
/// There was a problem with the database connection
|
||||
#[error("Connection Error: {0}")]
|
||||
Conn(String),
|
||||
/// There was a problem with the database connection from sqlx
|
||||
#[cfg(feature = "sqlx-dep")]
|
||||
#[error("Connection Error: {0}")]
|
||||
ConnSqlX(#[source] SqlxError),
|
||||
Conn(#[source] RuntimeErr),
|
||||
/// An operation did not execute successfully
|
||||
#[cfg(feature = "sqlx-dep")]
|
||||
#[error("Execution Error: {0}")]
|
||||
ExecSqlX(#[source] SqlxError),
|
||||
/// An error occurred while performing a query, with more details from sqlx
|
||||
#[cfg(feature = "sqlx-dep")]
|
||||
#[error("Query Error: {0}")]
|
||||
QuerySqlX(#[source] SqlxError),
|
||||
Exec(#[source] RuntimeErr),
|
||||
/// An error occurred while performing a query
|
||||
#[error("Query Error: {0}")]
|
||||
Query(String),
|
||||
Query(#[source] RuntimeErr),
|
||||
/// The record was not found in the database
|
||||
#[error("RecordNotFound Error: {0}")]
|
||||
RecordNotFound(String),
|
||||
@ -63,6 +54,18 @@ pub enum DbErr {
|
||||
Migration(String),
|
||||
}
|
||||
|
||||
/// Runtime error
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RuntimeErr {
|
||||
#[cfg(feature = "sqlx-dep")]
|
||||
/// SQLx Error
|
||||
#[error("{0}")]
|
||||
SqlxError(SqlxError),
|
||||
#[error("{0}")]
|
||||
/// Error generated from within SeaORM
|
||||
Internal(String),
|
||||
}
|
||||
|
||||
impl PartialEq for DbErr {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.to_string() == other.to_string()
|
||||
|
@ -41,7 +41,7 @@ impl From<TryGetError> for DbErr {
|
||||
match e {
|
||||
TryGetError::DbErr(e) => e,
|
||||
TryGetError::Null(s) => {
|
||||
DbErr::Query(format!("error occurred while decoding {}: Null", s))
|
||||
DbErr::Type(format!("A null value was encountered while decoding {}", s))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -627,7 +627,7 @@ where
|
||||
|
||||
fn try_get_many_with_slice_len_of(len: usize, cols: &[String]) -> Result<(), TryGetError> {
|
||||
if cols.len() < len {
|
||||
Err(TryGetError::DbErr(DbErr::Query(format!(
|
||||
Err(TryGetError::DbErr(DbErr::Type(format!(
|
||||
"Expect {} column names supplied but got slice of length {}",
|
||||
len,
|
||||
cols.len()
|
||||
|
Loading…
x
Reference in New Issue
Block a user