Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sxlijin opened this issue Oct 4, 2024 · 0 comments
Open

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

sxlijin opened this issue Oct 4, 2024 · 0 comments

Comments

@sxlijin
Copy link

sxlijin commented Oct 4, 2024

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))
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant