actix_diesel/
error.rs

1use derive_more::Display;
2use std::{error::Error as StdError, fmt::Debug};
3
4#[derive(Debug, Display)]
5pub enum AsyncError<E>
6where
7    E: 'static + Debug + Send + Sync,
8{
9    // Error occured when attempting to deliver the query to the Database actor
10    #[display(fmt = "{}", _0)]
11    Delivery(actix::MailboxError),
12
13    // Timed out trying to checkout a connection
14    #[display(fmt = "{}", _0)]
15    Timeout(r2d2::Error),
16
17    // An error occurred when interacting with the database
18    #[display(fmt = "{:?}", _0)]
19    Execute(E),
20}
21
22impl<E> StdError for AsyncError<E> where E: 'static + Debug + Send + Sync {}
23
24#[cfg(feature = "actix-web")]
25impl<E> actix_web::ResponseError for AsyncError<E> where E: 'static + Debug + Send + Sync {}
26
27#[cfg(feature = "failure")]
28impl AsyncError<failure::Error> {
29    /// Attempts to downcast this Error to a particular Fail type.
30    #[inline]
31    pub fn downcast<T: failure::Fail>(self) -> Result<T, Self> {
32        match self {
33            AsyncError::Execute(err) => err.downcast().map_err(AsyncError::Execute),
34            err => Err(err),
35        }
36    }
37
38    /// Attempts to downcast this Error to a particular Fail type by reference.
39    #[inline]
40    pub fn downcast_ref<T: failure::Fail>(&self) -> Option<&T> {
41        match self {
42            AsyncError::Execute(err) => err.downcast_ref(),
43            _ => None,
44        }
45    }
46
47    /// Attempts to downcast this Error to a particular Fail type by mutable reference.
48    #[inline]
49    pub fn downcast_mut<T: failure::Fail>(&mut self) -> Option<&mut T> {
50        match self {
51            AsyncError::Execute(err) => err.downcast_mut(),
52            _ => None,
53        }
54    }
55}