Enum ErrorData

Source
pub enum ErrorData<I> {
    Stashed(StashedErrors<I>),
    Wrapped(WrappedError<I>),
    AdHoc(AdHocError),
}
Expand description

Enum of all kinds of errors that you may want to return from functions when using this crate.

The main reason to use this crate is to return an arbitrary number of errors from functions, i.e. Result<_, StashedErrors>, where StashedErrors is basically a Vec<E>. At run-time, however, you may want to return some other error, for example when a guard clause evaluates to false or when a preliminary check evaluated to Err. In those cases, you can return an ad-hoc error or wrap that other error. This enum distinguishes these three cases. Since Error transparently wraps ErrorData, you can thus return Result<_, Error> in all of these cases.

Variants§

§

Stashed(StashedErrors<I>)

§

Wrapped(WrappedError<I>)

§

AdHoc(AdHocError)

Implementations§

Source§

impl<I> ErrorData<I>

Source

pub fn from_message<M: Display>(msg: M) -> Self

Creates an AdHocError variant of Error from a message.

Source

pub fn from_stash<M, E, L>(summary: M, errors: E, locations: L) -> Self
where M: Display, E: Into<Box<[I]>>, L: Into<Box<[&'static Location<'static>]>>,

Creates a StashedErrors variant of Error.

Source

pub fn wrap<E>(err: E) -> Self
where E: Into<I>,

Creates a WrappedError variant of Error from something that can be turned into an inner error type I.

Source

pub fn wrap_with<E, M>(err: E, msg: M) -> Self
where E: Into<I>, M: Display,

Creates a WrappedError variant of Error from something that can be turned into an inner error type I and annotates it with an informative message.

Source

pub fn childs(&self) -> &[I]

👎Deprecated since 0.6.0: renamed to children

Deprecated method that was renamed to children.

Source

pub fn children(&self) -> &[I]

Returns all errors that are direct children of this error.

#[cfg(any(feature = "rust-v1.81", feature = "std"))]
use lazy_errors::prelude::*;

#[cfg(not(any(feature = "rust-v1.81", feature = "std")))]
use lazy_errors::surrogate_error_trait::prelude::*;

let err = Error::from_message("Something went wrong");
assert!(err.children().is_empty());

let err = Error::wrap("A thing went wrong");
let e = match err.children() {
    [e] => e,
    _ => unreachable!(),
};
assert_eq!(&format!("{e}"), "A thing went wrong");

let mut err = ErrorStash::new(|| "One or more things went wrong");
err.push("An error");
err.push("Another error");

let r: Result<(), Error> = err.into();
let err = r.unwrap_err();
let [e1, e2] = match err.children() {
    [e1, e2] => [e1, e2],
    _ => unreachable!(),
};

assert_eq!(&format!("{e1}"), "An error");
assert_eq!(&format!("{e2}"), "Another error");

Note that this method only returns direct children. Each of those errors thus may have been created from an ErrorStash, which stored another level of errors. Such transitive children will not be returned from this method.

Trait Implementations§

Source§

impl<I> AsRef<ErrorData<I>> for Error<I>

Source§

fn as_ref(&self) -> &ErrorData<I>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<I: Debug> Debug for ErrorData<I>

Source§

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

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

impl<I: Display> Display for ErrorData<I>

Source§

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

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

impl<I: Display + Debug> Error for ErrorData<I>

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + '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<I> From<Error<I>> for ErrorData<I>

Source§

fn from(value: Error<I>) -> Self

Converts to this type from the input type.
Source§

impl<I> From<ErrorData<I>> for Error<I>

Source§

fn from(value: ErrorData<I>) -> Self

Converts to this type from the input type.
Source§

impl<I> Reportable for ErrorData<I>
where I: Display + Debug,

Auto Trait Implementations§

§

impl<I> Freeze for ErrorData<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for ErrorData<I>
where I: RefUnwindSafe,

§

impl<I> Send for ErrorData<I>
where I: Send,

§

impl<I> Sync for ErrorData<I>
where I: Sync,

§

impl<I> Unpin for ErrorData<I>
where I: Unpin,

§

impl<I> UnwindSafe for ErrorData<I>
where I: UnwindSafe,

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.