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

How can i handle multiple errors from Parallel execution #7

Open
Kirhchoff opened this issue Oct 20, 2023 · 3 comments
Open

How can i handle multiple errors from Parallel execution #7

Kirhchoff opened this issue Oct 20, 2023 · 3 comments

Comments

@Kirhchoff
Copy link

Kirhchoff commented Oct 20, 2023

I can't seem to figure out how to handle multiple errors occurring in parallel.

func fail(msg string) floc.Job {
	return func(ctx floc.Context, ctrl floc.Control) error {
		return errors.New("error " + msg)
	}
}

flow := run.Parallel(fail("asd"), fail("qwe"))
result, data, err := floc.Run(flow)

The err only contains one of the errors as far as I can tell. Seems like there is an ErrMultiple type, but I don't know how to use it.

Thank you for your help!

@workanator
Copy link
Owner

run.Parallel returns floc.ErrMultiple in case of any error occurs. Try "casting" the returned error to that type and access the list of errors here.

flow := run.Parallel(fail("asd"), fail("qwe"))
result, data, err := floc.Run(flow)
if err != nil {
  var multi floc.ErrMultiple
  if errors.As(err, &multi) {
    for _, errItem := range multi.List() {
      fmt.Println(errItem)
    }
  }
}

I cannot check the code I provided at the moment, but it shows the main idea.

@Kirhchoff
Copy link
Author

Thank you for the answer. It doesn't work though, because the err type i get is *errors.errorString for some reason, and the As function returns false.

@workanator
Copy link
Owner

Probably ErrMultiple lacks some implementations of interfaces required by errors package so errors.As cannot cast the error.

Try casting explicitly e, ok := err.(floc.ErrMultiple).

Anyway I would recommend using idiomatic Go instead of floc. There is a great support in Go and lots of great tools for making multiprocessing apps easily.

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

2 participants