Merge pull request #312 from SeaQL/detailed-conn-errors

Detailed connection errors
This commit is contained in:
Chris Tsang 2021-11-15 10:47:50 +08:00 committed by GitHub
commit 5f768aa436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 {
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;
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)),
}
}
}

View File

@ -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)),
}
}
}

View File

@ -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)),
}
}
}