From da705f6629db0dce4bf78de0bb6240285091fa9a Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 11 Nov 2021 16:58:11 +0800 Subject: [PATCH] Detailed connection errors --- src/driver/sqlx_common.rs | 5 +++++ src/driver/sqlx_mysql.rs | 9 ++++----- src/driver/sqlx_postgres.rs | 9 ++++----- src/driver/sqlx_sqlite.rs | 9 ++++----- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/driver/sqlx_common.rs b/src/driver/sqlx_common.rs index 5e035c9cb..18ca059d1 100644 --- a/src/driver/sqlx_common.rs +++ b/src/driver/sqlx_common.rs @@ -9,3 +9,8 @@ pub fn sqlx_error_to_exec_err(err: sqlx::Error) -> DbErr { pub fn sqlx_error_to_query_err(err: sqlx::Error) -> DbErr { DbErr::Query(err.to_string()) } + +/// Converts an [sqlx::error] connection error to a [DbErr] +pub fn sqlx_error_to_conn_err(err: sqlx::Error) -> DbErr { + DbErr::Conn(err.to_string()) +} diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index b2b89c680..3f9936e4d 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -41,12 +41,11 @@ impl SqlxMySqlConnector { use sqlx::ConnectOptions; opt.disable_statement_logging(); } - if let Ok(pool) = options.pool_options().connect_with(opt).await { - Ok(DatabaseConnection::SqlxMySqlPoolConnection( + match options.pool_options().connect_with(opt).await { + Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection( SqlxMySqlPoolConnection { pool }, - )) - } else { - Err(DbErr::Conn("Failed to connect.".to_owned())) + )), + Err(e) => Err(sqlx_error_to_conn_err(e)), } } } diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 84645a58d..3abebe318 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -41,12 +41,11 @@ impl SqlxPostgresConnector { use sqlx::ConnectOptions; opt.disable_statement_logging(); } - if let Ok(pool) = options.pool_options().connect_with(opt).await { - Ok(DatabaseConnection::SqlxPostgresPoolConnection( + match options.pool_options().connect_with(opt).await { + Ok(pool) => Ok(DatabaseConnection::SqlxPostgresPoolConnection( SqlxPostgresPoolConnection { pool }, - )) - } else { - Err(DbErr::Conn("Failed to connect.".to_owned())) + )), + Err(e) => Err(sqlx_error_to_conn_err(e)), } } } diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index 69eee5752..255686d2b 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -45,12 +45,11 @@ impl SqlxSqliteConnector { if options.get_max_connections().is_none() { options.max_connections(1); } - if let Ok(pool) = options.pool_options().connect_with(opt).await { - Ok(DatabaseConnection::SqlxSqlitePoolConnection( + match options.pool_options().connect_with(opt).await { + Ok(pool) => Ok(DatabaseConnection::SqlxSqlitePoolConnection( SqlxSqlitePoolConnection { pool }, - )) - } else { - Err(DbErr::Conn("Failed to connect.".to_owned())) + )), + Err(e) => Err(sqlx_error_to_conn_err(e)), } } }