Skip to content

Commit

Permalink
Refactor SqlxError;
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Aug 28, 2022
1 parent 348e841 commit 0ce0f49
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 27 deletions.
12 changes: 9 additions & 3 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())))
}
}
}

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
)))
))))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/driver/sqlx_common.rs
Original file line number Diff line number Diff line change
@@ -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))
}
2 changes: 1 addition & 1 deletion src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
27 changes: 15 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0ce0f49

Please sign in to comment.