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

In ErrorPool, store errors as []error instead of flattened error #143

Open
akallu opened this issue Nov 6, 2024 · 2 comments
Open

In ErrorPool, store errors as []error instead of flattened error #143

akallu opened this issue Nov 6, 2024 · 2 comments

Comments

@akallu
Copy link

akallu commented Nov 6, 2024

In the following snippet, we can see that errors are joined as they come in:

func (p *ErrorPool) addErr(err error) {
	if err != nil {
		p.mu.Lock()
		if p.onlyFirstError {
			if p.errs == nil {
				p.errs = err
			}
		} else {
			p.errs = multierror.Join(p.errs, err)
		}
		p.mu.Unlock()
	}
}

But this makes it incredibly difficult, without a recursive function, to pull the underlying errors out when consuming them from ErrorPool#Wait() (the current approach also stores the errors in a more suboptimal manner leading to more memory usage, but this isn't a huge deal):

func unwrapErrors(err error) []error {
	if err == nil {
		return nil
	}
	v, ok := err.(interface{ Unwrap() []error })
	if !ok {
		return []error{err}
	}
	var errs []error
	for _, wErr := range v.Unwrap() {
		errs = append(errs, unwrapErrors(wErr)...)
	}
	return errs
}

If the errors were kept as []error, and errors.Join(p.errs) was done inside ErrorPool#Wait(), it would be a 1-liner to pull the underlying errors out:

res, err := myPool.Wait()
errs := err.(interface{ Unwrap() []error }).Unwrap()

I don't mind doing the work for this, and it should be noted that it is backwards compatible w/ folks in the wild who have created their own functions like unwrapErrors above.

@akallu akallu changed the title In errorPool, stored errors as []error instead of error In ErrorPool, stored errors as []error instead of error Nov 6, 2024
@akallu akallu changed the title In ErrorPool, stored errors as []error instead of error In ErrorPool, store errors as []error instead of flattened error Nov 6, 2024
@akallu
Copy link
Author

akallu commented Dec 19, 2024

@camdencheek sorry to bother, but I saw you active in a prior issue. Any thoughts on whether I can move ahead with contributing this code? Would love to get this ironed out if possible, and thanks for creating such a great library!

@camdencheek
Copy link
Member

Hey @akallu, sorry for the delayed reponse. This is a great suggestion -- I'd be happy to accept a contribution here 🙂

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