From 0ce0f495511c981fd09bfd84578bf6f97c45f32b Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 28 Aug 2022 13:51:21 +0800 Subject: [PATCH] Refactor SqlxError; --- src/database/db_connection.rs | 12 +++++++++--- src/database/mod.rs | 6 +++--- src/driver/sqlx_common.rs | 8 ++++---- src/driver/sqlx_mysql.rs | 2 +- src/driver/sqlx_postgres.rs | 2 +- src/driver/sqlx_sqlite.rs | 2 +- src/error.rs | 27 +++++++++++++++------------ src/executor/query.rs | 4 ++-- 8 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/database/db_connection.rs b/src/database/db_connection.rs index ac4fd7e62..3fa48ec0e 100644 --- a/src/database/db_connection.rs +++ b/src/database/db_connection.rs @@ -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()))) + } } } diff --git a/src/database/mod.rs b/src/database/mod.rs index 4a24ec88d..22a173c57 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -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 - ))) + )))) } } diff --git a/src/driver/sqlx_common.rs b/src/driver/sqlx_common.rs index 915098136..37a250ed3 100644 --- a/src/driver/sqlx_common.rs +++ b/src/driver/sqlx_common.rs @@ -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)) } diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index e1f1d4169..c15197a41 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -45,7 +45,7 @@ impl SqlxMySqlConnector { let mut opt = options .url .parse::() - .map_err(|e| DbErr::ConnSqlX(e))?; + .map_err(sqlx_error_to_conn_err)?; use sqlx::ConnectOptions; if !options.sqlx_logging { opt.disable_statement_logging(); diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 6d070115d..78cc4f3cb 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -45,7 +45,7 @@ impl SqlxPostgresConnector { let mut opt = options .url .parse::() - .map_err(|e| DbErr::ConnSqlX(e))?; + .map_err(sqlx_error_to_conn_err)?; use sqlx::ConnectOptions; if !options.sqlx_logging { opt.disable_statement_logging(); diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 55e768b0d..f2b17a95a 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -46,7 +46,7 @@ impl SqlxSqliteConnector { let mut opt = options .url .parse::() - .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()); } diff --git a/src/error.rs b/src/error.rs index a5db227eb..26bd5ddc2 100644 --- a/src/error.rs +++ b/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() diff --git a/src/executor/query.rs b/src/executor/query.rs index c2d0a051c..f27b1a2d9 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -41,7 +41,7 @@ impl From 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()