Skip to content

Commit cc6a0ad

Browse files
committed
Add back Error::as_db_error
Closes #732
1 parent 096f4f5 commit cc6a0ad

File tree

1 file changed

+9
-5
lines changed
  • tokio-postgres/src/error

1 file changed

+9
-5
lines changed

tokio-postgres/src/error/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,18 @@ impl Error {
414414
self.0.cause
415415
}
416416

417+
/// Returns the source of this error if it was a `DbError`.
418+
///
419+
/// This is a simple convenience method.
420+
pub fn as_db_error(&self) -> Option<&DbError> {
421+
self.source().and_then(|e| e.downcast_ref::<DbError>())
422+
}
423+
417424
/// Returns the SQLSTATE error code associated with the error.
418425
///
419-
/// This is a convenience method that downcasts the cause to a `DbError`
420-
/// and returns its code.
426+
/// This is a convenience method that downcasts the cause to a `DbError` and returns its code.
421427
pub fn code(&self) -> Option<&SqlState> {
422-
self.source()
423-
.and_then(|e| e.downcast_ref::<DbError>())
424-
.map(DbError::code)
428+
self.as_db_error().map(DbError::code)
425429
}
426430

427431
fn new(kind: Kind, cause: Option<Box<dyn error::Error + Sync + Send>>) -> Error {

0 commit comments

Comments
 (0)