Detailed connection errors

This commit is contained in:
Billy Chan 2021-11-11 16:58:11 +08:00
parent 64c54f8ad6
commit da705f6629
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
4 changed files with 17 additions and 15 deletions

View File

@ -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 { pub fn sqlx_error_to_query_err(err: sqlx::Error) -> DbErr {
DbErr::Query(err.to_string()) 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())
}

View File

@ -41,12 +41,11 @@ impl SqlxMySqlConnector {
use sqlx::ConnectOptions; use sqlx::ConnectOptions;
opt.disable_statement_logging(); opt.disable_statement_logging();
} }
if let Ok(pool) = options.pool_options().connect_with(opt).await { match options.pool_options().connect_with(opt).await {
Ok(DatabaseConnection::SqlxMySqlPoolConnection( Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection(
SqlxMySqlPoolConnection { pool }, SqlxMySqlPoolConnection { pool },
)) )),
} else { Err(e) => Err(sqlx_error_to_conn_err(e)),
Err(DbErr::Conn("Failed to connect.".to_owned()))
} }
} }
} }

View File

@ -41,12 +41,11 @@ impl SqlxPostgresConnector {
use sqlx::ConnectOptions; use sqlx::ConnectOptions;
opt.disable_statement_logging(); opt.disable_statement_logging();
} }
if let Ok(pool) = options.pool_options().connect_with(opt).await { match options.pool_options().connect_with(opt).await {
Ok(DatabaseConnection::SqlxPostgresPoolConnection( Ok(pool) => Ok(DatabaseConnection::SqlxPostgresPoolConnection(
SqlxPostgresPoolConnection { pool }, SqlxPostgresPoolConnection { pool },
)) )),
} else { Err(e) => Err(sqlx_error_to_conn_err(e)),
Err(DbErr::Conn("Failed to connect.".to_owned()))
} }
} }
} }

View File

@ -45,12 +45,11 @@ impl SqlxSqliteConnector {
if options.get_max_connections().is_none() { if options.get_max_connections().is_none() {
options.max_connections(1); options.max_connections(1);
} }
if let Ok(pool) = options.pool_options().connect_with(opt).await { match options.pool_options().connect_with(opt).await {
Ok(DatabaseConnection::SqlxSqlitePoolConnection( Ok(pool) => Ok(DatabaseConnection::SqlxSqlitePoolConnection(
SqlxSqlitePoolConnection { pool }, SqlxSqlitePoolConnection { pool },
)) )),
} else { Err(e) => Err(sqlx_error_to_conn_err(e)),
Err(DbErr::Conn("Failed to connect.".to_owned()))
} }
} }
} }