Enum Error

Source
pub enum Error<D> {
    Domain(Box<D>),
    Fault(Fault),
}
Expand description

Use Result<T, explicit_error::Error> as the return type of any binary crate faillible function returning errors. The Error::Fault variant is for errors that should not happen but cannot panic. The Error::Domain variant is for domain errors that provide feedbacks to the user. For library or functions that require the caller to pattern match on the returned error, a dedicated type is prefered.

Variants§

§

Domain(Box<D>)

§

Fault(Fault)

Implementations§

Source§

impl<D> Error<D>
where D: StdError + 'static,

Source

pub fn is_domain(&self) -> bool

Return true if it’s a Error::Domain variant

Source

pub fn is_fault(&self) -> bool

Return true if it’s a Error::Fault variant

Source

pub fn unwrap(self) -> D

Unwrap the Error::Domain variant, panic otherwise

Source

pub fn unwrap_fault(self) -> Fault

Unwrap the Error::Fault variant, panic otherwise

Source

pub fn downcast_source_ref<E>(&self) -> Option<&E>
where E: StdError + 'static,

Try to downcast the source of the type wrapped in either Error::Domain or Error::Fault variant. If it is not set try to downcast the type wrapped. Usefull to assert_eq! in tests

§Examples
use explicit_error_exit::{ExitError, derive::ExitError, Error};
#[test]
fn test() {
    assert_eq!(to_test().unwrap_err().downcast_source_ref()::<MyError>().unwrap(), &MyError::Foo);
}

#[derive(ExitError, Debug)]
enum MyError {
    Foo,
}


fn to_test() -> Result<(), Error> {
    Err(MyError::Foo)?;
    Ok(())
}
Source§

impl<D> Error<D>
where D: Domain,

Source

pub fn downcast_source<E>( self, ) -> Result<E, Box<dyn Error + Send + Sync + 'static>>
where E: StdError + 'static,

Try to downcast the source of the type wrapped in either Error::Domain or Error::Fault variant. If it is not set try to downcast the type wrapped. Usefull to assert_eq! in tests

§Examples
use explicit_error_exit::{ExitError, derive::ExitError, Error};
#[test]
fn test() {
    assert_eq!(to_test().unwrap_err().downcast_source::<MyError>().unwrap(), MyError::Foo);
}

#[derive(ExitError, Debug)]
enum MyError {
    Foo,
}


fn to_test() -> Result<(), Error> {
    Err(MyError::Foo)?;
    Ok(())
}
Source

pub fn with_context(self, context: impl Display) -> Self

Add context of either Error::Domain or Error::Fault variant. Override existing context

Source

pub fn context(&self) -> Option<&str>

Return the context of either Error::Domain or Error::Fault variant.

Trait Implementations§

Source§

impl<D: Debug> Debug for Error<D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D> Display for Error<D>
where D: StdError,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<D> Error for Error<D>
where D: StdError + 'static,

Source§

fn source(&self) -> Option<&(dyn StdError + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<D> From<Fault> for Error<D>

Source§

fn from(value: Fault) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<D> !Freeze for Error<D>

§

impl<D> !RefUnwindSafe for Error<D>

§

impl<D> Send for Error<D>
where D: Send,

§

impl<D> Sync for Error<D>
where D: Sync,

§

impl<D> Unpin for Error<D>

§

impl<D> !UnwindSafe for Error<D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.