Skip to content

Feature request: ability to set default formatter #62

@ryanmcnamara

Description

@ryanmcnamara

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions