feat: re-introduce error downcasting

This commit is contained in:
Ryan Leckey
2020-05-30 15:55:04 -07:00
parent e5b6047009
commit e08f05b879
8 changed files with 153 additions and 4 deletions

View File

@@ -89,6 +89,20 @@ pub enum Error {
}
impl Error {
pub fn into_database_error(self) -> Option<Box<dyn DatabaseError + 'static>> {
match self {
Error::Database(err) => Some(err),
_ => None,
}
}
pub fn as_database_error(&self) -> Option<&(dyn DatabaseError + 'static)> {
match self {
Error::Database(err) => Some(&**err),
_ => None,
}
}
#[allow(dead_code)]
#[inline]
pub(crate) fn protocol(err: impl Display) -> Self {
@@ -124,6 +138,69 @@ pub trait DatabaseError: 'static + Send + Sync + StdError {
fn code(&self) -> Option<Cow<str>> {
None
}
#[doc(hidden)]
fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static);
#[doc(hidden)]
fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static);
#[doc(hidden)]
fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static>;
}
impl dyn DatabaseError {
/// Downcast a reference to this generic database error to a specific
/// database error type.
///
/// # Panics
///
/// Panics if the database error type is not `E`. This is a deliberate contrast from
/// `Error::downcast_ref` which returns `Option<&E>`. In normal usage, you should know the
/// specific error type. In other cases, use `try_downcast_ref`.
///
pub fn downcast_ref<E: DatabaseError>(&self) -> &E {
self.try_downcast_ref().unwrap_or_else(|| {
panic!(
"downcast to wrong DatabaseError type; original error: {}",
self
)
})
}
/// Downcast this generic database error to a specific database error type.
///
/// # Panics
///
/// Panics if the database error type is not `E`. This is a deliberate contrast from
/// `Error::downcast` which returns `Option<E>`. In normal usage, you should know the
/// specific error type. In other cases, use `try_downcast`.
///
pub fn downcast<E: DatabaseError>(self: Box<Self>) -> Box<E> {
self.try_downcast().unwrap_or_else(|e| {
panic!(
"downcast to wrong DatabaseError type; original error: {}",
e
)
})
}
/// Downcast a reference to this generic database error to a specific
/// database error type.
#[inline]
pub fn try_downcast_ref<E: DatabaseError>(&self) -> Option<&E> {
self.as_error().downcast_ref()
}
/// Downcast this generic database error to a specific database error type.
#[inline]
pub fn try_downcast<E: DatabaseError>(self: Box<Self>) -> StdResult<Box<E>, Box<Self>> {
if self.as_error().is::<E>() {
Ok(self.into_error().downcast().unwrap())
} else {
Err(self)
}
}
}
impl<E> From<E> for Error

View File

@@ -61,4 +61,19 @@ impl DatabaseError for MySqlDatabaseError {
fn code(&self) -> Option<Cow<str>> {
self.code().map(Cow::Borrowed)
}
#[doc(hidden)]
fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn as_error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn into_error(self: Box<Self>) -> Box<dyn Error + Send + Sync + 'static> {
self
}
}

View File

@@ -2,10 +2,10 @@ use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};
use atoi::atoi;
use smallvec::alloc::borrow::Cow;
use crate::error::DatabaseError;
use crate::postgres::message::{Notice, PgSeverity};
use smallvec::alloc::borrow::Cow;
/// An error returned from the PostgreSQL database.
pub struct PgDatabaseError(pub(crate) Notice);
@@ -115,7 +115,7 @@ impl PgDatabaseError {
}
}
#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub enum PgErrorPosition<'a> {
/// A position (in characters) into the original query.
Original(usize),
@@ -169,4 +169,19 @@ impl DatabaseError for PgDatabaseError {
fn code(&self) -> Option<Cow<str>> {
Some(Cow::Borrowed(self.code()))
}
#[doc(hidden)]
fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn as_error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn into_error(self: Box<Self>) -> Box<dyn Error + Send + Sync + 'static> {
self
}
}

View File

@@ -6,7 +6,7 @@ use memchr::memchr;
use crate::error::Error;
use crate::io::Decode;
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum PgSeverity {
Panic,

View File

@@ -50,4 +50,19 @@ impl DatabaseError for SqliteError {
fn message(&self) -> &str {
&self.message
}
#[doc(hidden)]
fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
self
}
}