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§
Implementations§
Source§impl<I> ErrorData<I>
impl<I> ErrorData<I>
Sourcepub fn from_message<M: Display>(msg: M) -> Self
pub fn from_message<M: Display>(msg: M) -> Self
Creates an AdHocError
variant of Error
from a message.
Sourcepub fn from_stash<M, E, L>(summary: M, errors: E, locations: L) -> Self
pub fn from_stash<M, E, L>(summary: M, errors: E, locations: L) -> Self
Creates a StashedErrors
variant of Error
.
Sourcepub fn wrap<E>(err: E) -> Selfwhere
E: Into<I>,
pub fn wrap<E>(err: E) -> Selfwhere
E: Into<I>,
Creates a WrappedError
variant of Error
from something that can be turned into an
inner error type I
.
Sourcepub fn wrap_with<E, M>(err: E, msg: M) -> Self
pub fn wrap_with<E, M>(err: E, msg: M) -> Self
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.
Sourcepub fn childs(&self) -> &[I]
👎Deprecated since 0.6.0: renamed to children
pub fn childs(&self) -> &[I]
children
Deprecated method that was renamed to
children
.
Sourcepub fn children(&self) -> &[I]
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.