pub struct Many {
pub message: Cow<'static, str>,
pub causes: Vec<Box<dyn Error + Send + Sync + 'static>>,
pub location: Option<&'static Location<'static>>,
}Expand description
An error type that can have multiple simultaneous causes.
Unlike scoped_error::Error which has a single causal chain,
Many stores multiple independent errors. This is useful for:
- Parallel operations where multiple tasks may fail
- Validation that collects all errors before reporting
- Operations with multiple independent failure modes
The source method returns the first cause for
compatibility with the standard error interface. Use
causes or the tree-formatted ErrorReport to
access all errors.
§Example
use scoped_error::{Error, Many, expect_error};
use std::thread;
fn parallel_work() -> Result<Vec<()>, Many> {
let handles = vec![
thread::spawn(|| task_a()),
thread::spawn(|| task_b()),
];
let results: Vec<_> = handles
.into_iter()
.map(|h| h.join().unwrap())
.collect();
Many::from_results("parallel tasks failed", results)
}
fn task_a() -> Result<(), Error> {
expect_error("task A failed", || {
some_fallible_op()?;
Ok(())
})
}
fn task_b() -> Result<(), Error> {
expect_error("task B failed", || {
some_fallible_op()?;
Ok(())
})
}
Fields§
§message: Cow<'static, str>The primary error message describing the overall failure.
causes: Vec<Box<dyn Error + Send + Sync + 'static>>All independent causes of this error.
location: Option<&'static Location<'static>>Where this error was created.
Implementations§
Source§impl Many
impl Many
Sourcepub fn new(msg: impl Into<Cow<'static, str>>) -> Self
pub fn new(msg: impl Into<Cow<'static, str>>) -> Self
Create a new Many with the given message.
The causes list starts empty. Use with_cause
or from_results to populate it.
§Example
use scoped_error::Many;
let err = Many::new("validation failed");Sourcepub fn with_cause<E>(self, cause: E) -> Self
pub fn with_cause<E>(self, cause: E) -> Self
Add a cause to this error.
Returns self for chaining.
§Example
use scoped_error::Many;
let err = Many::new("multiple failures")
.with_cause(std::io::Error::other("disk full"))
.with_cause(std::io::Error::other("network timeout"));Sourcepub fn causes(&self) -> &[Box<dyn Error + Send + Sync + 'static>]
pub fn causes(&self) -> &[Box<dyn Error + Send + Sync + 'static>]
Get all causes as a slice.
Use this to iterate over all errors when the tree-formatted report doesn’t provide enough control.
§Example
use scoped_error::Many;
let err = Many::new("example");
for (i, cause) in err.causes().iter().enumerate() {
println!("Failure {}: {}", i, cause);
}Sourcepub fn from_results<T, E>(
msg: impl Into<Cow<'static, str>>,
results: impl IntoIterator<Item = Result<T, E>>,
) -> Result<Vec<T>, Self>
pub fn from_results<T, E>( msg: impl Into<Cow<'static, str>>, results: impl IntoIterator<Item = Result<T, E>>, ) -> Result<Vec<T>, Self>
Collect results, returning Ok if all succeeded, Err with all failures.
This is the primary way to construct a Many from
multiple operations. If all results are Ok, returns Ok with
all the success values. If any are Err, returns Err with a
Many containing all the errors.
§Type Parameters
T: The success type of each resultE: The error type (must be convertible to the boxed error type)
§Example
use scoped_error::Many;
let results: Vec<Result<i32, std::io::Error>> = vec![
Ok(1),
Err(std::io::Error::other("fail 1")),
Err(std::io::Error::other("fail 2")),
];
match Many::from_results("batch operation failed", results) {
Ok(values) => println!("Success: {:?}", values),
Err(e) => println!("{}\nCaused by {} errors", e, e.causes().len()),
}Sourcepub fn from_errors<E>(
msg: impl Into<Cow<'static, str>>,
errors: impl IntoIterator<Item = E>,
) -> Self
pub fn from_errors<E>( msg: impl Into<Cow<'static, str>>, errors: impl IntoIterator<Item = E>, ) -> Self
Create from an iterator of errors, with a message.
Unlike from_results, this always returns Err. Use when you
already know you have failures to report.
§Example
use scoped_error::Many;
let errors: Vec<std::io::Error> = vec![
std::io::Error::other("error 1"),
std::io::Error::other("error 2"),
];
let err = Many::from_errors("validation failed", errors);Examples found in repository?
28fn validate_user(input: &UserInput) -> Result<(), Many> {
29 let mut errors = Vec::new();
30
31 if input.name.is_empty() {
32 errors.push(ValidationError::new("name is required"));
33 }
34 if input.email.is_empty() {
35 errors.push(ValidationError::new("email is required"));
36 }
37 if input.age < 18 {
38 errors.push(ValidationError::new("must be 18 or older"));
39 }
40
41 if errors.is_empty() {
42 Ok(())
43 } else {
44 Err(Many::from_errors("user validation failed", errors))
45 }
46}Trait Implementations§
Source§impl Error for Many
impl Error for Many
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the first cause, if any.
This provides compatibility with the standard Error trait’s
single-chain model. To access all causes, use causes
or the tree-formatted error report.
Returns None if there are no causes.
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()