Documentation
¶
Overview ¶
Package multierror can be leveraged as an opinionated to handle multiple errors providing appropriate wrapping for them.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JSONFormatFunc ¶ added in v1.1.0
JSONFormatFunc takes in a list of errors and encodes each error to JSON. If the error is not encodable to JSON, then the error is transformed to the json format: `{"message": "err.Error()"}`.
func WithFormat ¶ added in v1.1.0
WithFormat is convenient a helper to modify the multierror format function to the JSON format, if the format isn't "json", then the unmodified error is returned.
func WithFormatFunc ¶ added in v1.1.0
func WithFormatFunc(err error, ff FormatFunc) error
WithFormatFunc takes in an error and tries to set the ErrorFormatFunc to the passed function if the error is of type *Prefixed or *multierror.Error, otherwise it returns the error as is.
Types ¶
type FormatFunc ¶
FormatFunc defines a format function which should format a slice of errors into a string.
type JSONError ¶ added in v1.1.0
type JSONError struct {
Errors []interface{} `json:"errors,omitempty"`
}
JSONError wraps a list of errors to be encoded in JSON format.
type Prefixed ¶
type Prefixed struct { Prefix string Errors []error FormatFunc FormatFunc // SkipPrefixing when set to true subsequently appended errors needn't be // prefixed. This is particularly useful multierrors which contain JSON // marshaleable errors. SkipPrefixing bool // contains filtered or unexported fields }
Prefixed is a multierror which will prefix the error output message with the specified prefix. The output will be sorted so the errors can be asserted on test functions in case the multierror has been used in different goroutines. This structure is concurrently safe, avoid using value semantics.
Example ¶
err := NewPrefixed("config validation") err = err.Append(errors.New("some validation error")) if err.ErrorOrNil() != nil { fmt.Fprintln(output, err) }
Output:
Example (Json) ¶
err := NewPrefixed("config validation") err = err.Append(errors.New("some validation error")) if err.ErrorOrNil() != nil { fmt.Fprintln(output, WithFormat(err, "json")) }
Output:
func NewJSONPrefixed ¶ added in v1.1.0
NewJSONPrefixed returns a new pointer to Prefixed with the right config for JSON packed errors to not be prefixed.
func NewPrefixed ¶
NewPrefixed creates a new pointer to Prefixed.
func (*Prefixed) Append ¶
Append appends a number of errors to the current instance of Prefixed. It'll unwrap any wrapped errors in the form of *Prefixed or *multierror.Error.
func (*Prefixed) Error ¶
Error returns the stored slice of error formatted using a set FormatFunc or multierror.ListFormatFunc when no FormatFunc is specified.
func (*Prefixed) ErrorOrNil ¶
ErrorOrNil either returns nil when the type is nil or when there's no Errors. Otherwise, the type is returned.