Enhance error handling in DbError conversion: Improved the From<PgError> implementation to provide detailed error messages, including SQLSTATE, detail, and hint when available, for better debugging and clarity in database error reporting.

This commit is contained in:
Torsten Schulz (local)
2025-11-25 14:21:42 +01:00
parent 5f44f50304
commit 25f2eb150d

View File

@@ -32,7 +32,19 @@ impl std::error::Error for DbError {}
impl From<PgError> for DbError { impl From<PgError> for DbError {
fn from(err: PgError) -> Self { fn from(err: PgError) -> Self {
DbError::new(format!("Postgres-Fehler: {err}")) if let Some(db_err) = err.as_db_error() {
let code = db_err.code();
let message = db_err.message();
let detail = db_err.detail().unwrap_or_default();
let hint = db_err.hint().unwrap_or_default();
DbError::new(format!(
"Postgres-Fehler: {} (SQLSTATE: {}, Detail: {}, Hint: {})",
message, code, detail, hint
))
} else {
DbError::new(format!("Postgres-Fehler (Client): {err}"))
}
} }
} }