feat: re-introduce error downcasting

This commit is contained in:
Ryan Leckey 2020-05-30 15:55:04 -07:00
parent e5b6047009
commit e08f05b879
No known key found for this signature in database
GPG key ID: BBDFC5595030E7D3
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
}
}

View file

@ -8,6 +8,7 @@ services:
- "$HOME/.cargo/registry:/usr/local/cargo/registry"
working_dir: "/home/rust/src"
environment:
CARGO_TARGET_DIR: "/home/rust/src/tests/target"
CARGO_INCREMENTAL: "0"
RUSTFLAGS: "-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
RUSTDOCFLAGS: "-Cpanic=abort"

View file

@ -1,6 +1,7 @@
use futures::TryStreamExt;
use sqlx::postgres::PgRow;
use sqlx::{postgres::Postgres, Executor, Row};
use sqlx_core::postgres::{PgDatabaseError, PgErrorPosition, PgSeverity};
use sqlx_test::new;
#[sqlx_macros::test]
@ -32,6 +33,31 @@ async fn it_maths() -> anyhow::Result<()> {
Ok(())
}
#[sqlx_macros::test]
async fn it_can_inspect_errors() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let res: Result<u64, sqlx::Error> = sqlx::query("select f").execute(&mut conn).await;
let err = res.unwrap_err();
// can also do [as_database_error] or use `match ..`
let err = err.into_database_error().unwrap();
assert_eq!(err.message(), "column \"f\" does not exist");
assert_eq!(err.code().as_deref(), Some("42703"));
// can also do [downcast_ref]
let err: Box<PgDatabaseError> = err.downcast();
assert_eq!(err.severity(), PgSeverity::Error);
assert_eq!(err.message(), "column \"f\" does not exist");
assert_eq!(err.code(), "42703");
assert_eq!(err.position(), Some(PgErrorPosition::Original(8)));
assert_eq!(err.routine(), Some("errorMissingColumn"));
Ok(())
}
#[sqlx_macros::test]
async fn it_executes() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;

View file

@ -85,7 +85,7 @@ def run(command, comment=None, env=None, service=None, tag=None, args=None):
# before we start, we clean previous profile data
# keeping these around can cause weird errors
for path in glob(os.path.join(os.path.dirname(__file__), "../target/**/*.gc*"), recursive=True):
for path in glob(os.path.join(os.path.dirname(__file__), "target/**/*.gc*"), recursive=True):
os.remove(path)
#