Skip to content

Feature request / discuss: allow errors to have multiple causes #387

@sxlijin

Description

@sxlijin

I've run into multiple situations now where X can only succeed if A, B, and C all succeed, but executing each of A/B/C happens independently, so I'd like to be able to do something like the below example.

Using contexts doesn't really make sense here, because A/B/C failing are all independent of each other, they don't cause one another, and contexts are meant to be used for "error 1 happened because of error 2"

Have you ever considered doing this? Do you know anyone who's made a crate like this? This doesn't appear to be something that anyhow/thiserror/snafu support, but it feels like something that could reasonably fit into anyhow.

// this is pseudo-code, meant to illustrate the _sentiment_ of what i want, not the actual API
fn x() -> anyhow::Result<()> {
  let mut errs = vec![];
  match a() {
    Ok(_) => ...,
    Err(e) => { errs.push(e); },
  }
  match b() {
    Ok(_) => ...,
    Err(e) => { errs.push(e); },
  }
  match c() {
    Ok(_) => ...,
    Err(e) => { errs.push(e); },
  }
  if errs.empty() {
    Ok(())
  } else {
    Err(anyhow::Error::sum(errs))
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions