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: ability to set default formatter #62

Open
ryanmcnamara opened this issue Jul 26, 2022 · 1 comment
Open

Feature request: ability to set default formatter #62

ryanmcnamara opened this issue Jul 26, 2022 · 1 comment

Comments

@ryanmcnamara
Copy link

Hi, thanks for maintaining this library

I'd like to use this library to return errors to a caller that would prefer no newlines if possible (in the event of one error). That is doable and looks like this:

func Foo() error {
	var retErr *multierror.Error
	for _, x := range getSlice() {
		if err := bar(x); err != nil {
			retErr = multierror.Append(retErr, err)
		}
	}
	return decorateFormat(retErr.ErrorOrNil())
}

func decorateFormat(err *multierror.Error) *multierror.Error {
	if err != nil {
		err.ErrorFormat = multierror.ErrorFormatFunc(func(errs []error) string {
			if len(errs) == 1 {
				return errs[0].Error()
			}
			return multierror.ListFormatFunc(errs)
		})
	}
	return err
}

Which is fine, but requires me to remember to set the listFormatFunc, I'm doing that in a separate "decorator" func because I will need to do this many times.

I would like to make it possible to specify the "default" error func so I don't have to remember to set it. This way, whenever a new multierror.Error was created, it would automatically have the format function I want. Would you accept a change like that? If so, perhaps a default global (defaults to the current ListFormatFunc) is the way to go, but would defer to you. Thanks in advance!

@dolmen
Copy link

dolmen commented Mar 24, 2024

Your decorateFormat func should return error in its signature instead of *multierror.Error to avoid returning (*multierror.Error)(nil) from Foo.

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