[][src]Struct fehler::Exception

pub struct Exception { /* fields omitted */ }

The `Exception type, a wrapper around a dynamic error type.

Exception functions a lot like Box<dyn Error>, with these differences:

  • Exception requires that the error is Send, Sync, and 'static
  • Exception guarantees that a backtrace will exist, even if the error type did not provide one
  • Exception is represented as a narrow pointer - exactly one word in size, instead of two.

Methods

impl Exception[src]

pub fn new<E>(error: E) -> Exception where
    E: Error + Send + Sync + 'static, 
[src]

Create a new exception from any error type.

The error type must be threadsafe and 'static, so that the Exception will be as well.

If the error type does not provide a backtrace, a backtrace will be created here to ensure that a backtrace exists.

pub fn as_error(&self) -> &(dyn Error + Send + Sync + 'static)[src]

View this exception as the underlying error.

pub fn as_error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static)[src]

View this exception as the underlying error, mutably.

pub fn backtrace(&self) -> &Backtrace[src]

Get the backtrace for this Exception.

Important traits for Errors<'a>
pub fn errors(&self) -> Errors[src]

An iterator of errors contained by this Exception.

This iterator will visit every error in the "cause chain" of this exception, beginning with the error that this exception was created from.

pub fn is<E: Display + Debug + Send + Sync + 'static>(&self) -> bool[src]

Returns true if E is the type wrapped by this exception.

pub fn downcast<E: Display + Debug + Send + Sync + 'static>(
    self
) -> Result<E, Exception>
[src]

Attempt to downcast the exception to a concrete type.

pub fn downcast_ref<E: Display + Debug + Send + Sync + 'static>(
    &self
) -> Option<&E>
[src]

Downcast this exception by reference.

pub fn downcast_mut<E: Display + Debug + Send + Sync + 'static>(
    &mut self
) -> Option<&mut E>
[src]

Downcast this exception by mutable reference.

Methods from Deref<Target = dyn Error + Send + Sync + 'static>

pub fn is<T>(&self) -> bool where
    T: 'static + Error
1.3.0[src]

Returns true if the boxed type is the same as T

pub fn downcast_ref<T>(&self) -> Option<&T> where
    T: 'static + Error
1.3.0[src]

Returns some reference to the boxed value if it is of type T, or None if it isn't.

pub fn downcast_mut<T>(&mut self) -> Option<&mut T> where
    T: 'static + Error
1.3.0[src]

Returns some mutable reference to the boxed value if it is of type T, or None if it isn't.

pub fn is<T>(&self) -> bool where
    T: 'static + Error
1.3.0[src]

Forwards to the method defined on the type Any.

pub fn downcast_ref<T>(&self) -> Option<&T> where
    T: 'static + Error
1.3.0[src]

Forwards to the method defined on the type Any.

pub fn downcast_mut<T>(&mut self) -> Option<&mut T> where
    T: 'static + Error
1.3.0[src]

Forwards to the method defined on the type Any.

pub fn is<T>(&self) -> bool where
    T: 'static + Error
1.3.0[src]

Forwards to the method defined on the type Any.

pub fn downcast_ref<T>(&self) -> Option<&T> where
    T: 'static + Error
1.3.0[src]

Forwards to the method defined on the type Any.

pub fn downcast_mut<T>(&mut self) -> Option<&mut T> where
    T: 'static + Error
1.3.0[src]

Forwards to the method defined on the type Any.

pub fn iter_chain(&self) -> ErrorIter[src]

🔬 This is a nightly-only experimental API. (error_iter)

Returns an iterator starting with the current error and continuing with recursively calling source.

Examples

#![feature(error_iter)]
use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct A;

#[derive(Debug)]
struct B(Option<Box<dyn Error + 'static>>);

impl fmt::Display for A {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "A")
    }
}

impl fmt::Display for B {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "B")
    }
}

impl Error for A {}

impl Error for B {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.0.as_ref().map(|e| e.as_ref())
    }
}

let b = B(Some(Box::new(A)));

// let err : Box<Error> = b.into(); // or
let err = &b as &(dyn Error);

let mut iter = err.iter_chain();

assert_eq!("B".to_string(), iter.next().unwrap().to_string());
assert_eq!("A".to_string(), iter.next().unwrap().to_string());
assert!(iter.next().is_none());
assert!(iter.next().is_none());

pub fn iter_sources(&self) -> ErrorIter[src]

🔬 This is a nightly-only experimental API. (error_iter)

Returns an iterator starting with the source of this error and continuing with recursively calling source.

Examples

#![feature(error_iter)]
use std::error::Error;
use std::fmt;

#[derive(Debug)]
struct A;

#[derive(Debug)]
struct B(Option<Box<dyn Error + 'static>>);

#[derive(Debug)]
struct C(Option<Box<dyn Error + 'static>>);

impl fmt::Display for A {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "A")
    }
}

impl fmt::Display for B {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "B")
    }
}

impl fmt::Display for C {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "C")
    }
}

impl Error for A {}

impl Error for B {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.0.as_ref().map(|e| e.as_ref())
    }
}

impl Error for C {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.0.as_ref().map(|e| e.as_ref())
    }
}

let b = B(Some(Box::new(A)));
let c = C(Some(Box::new(b)));

// let err : Box<Error> = c.into(); // or
let err = &c as &(dyn Error);

let mut iter = err.iter_sources();

assert_eq!("B".to_string(), iter.next().unwrap().to_string());
assert_eq!("A".to_string(), iter.next().unwrap().to_string());
assert!(iter.next().is_none());
assert!(iter.next().is_none());

Trait Implementations

impl AsError for Exception[src]

impl<T> Context<T, Exception> for Result<T, Exception>[src]

impl<E: Error + Send + Sync + 'static> From<E> for Exception[src]

impl Send for Exception[src]

impl Sync for Exception[src]

impl Drop for Exception[src]

impl Deref for Exception[src]

type Target = dyn Error + Send + Sync + 'static

The resulting type after dereferencing.

impl DerefMut for Exception[src]

impl Debug for Exception[src]

impl Display for Exception[src]

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]