Add CombineErrors helper function
This commit is contained in:
parent
c6311c1e32
commit
94de87d4b7
1 changed files with 31 additions and 0 deletions
|
@ -2,6 +2,7 @@ package errors
|
|||
|
||||
import (
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
@ -22,12 +23,42 @@ var Wrap = errors.Wrap
|
|||
// nil, Wrapf returns nil.
|
||||
var Wrapf = errors.Wrapf
|
||||
|
||||
// WithStack annotates err with a stack trace at the point WithStack was called.
|
||||
// If err is nil, WithStack returns nil.
|
||||
var WithStack = errors.WithStack
|
||||
|
||||
// Go 1.13-style error handling.
|
||||
|
||||
// As finds the first error in err's tree that matches target, and if one is found,
|
||||
// sets target to that error value and returns true. Otherwise, it returns false.
|
||||
func As(err error, tgt interface{}) bool { return stderrors.As(err, tgt) }
|
||||
|
||||
// Is reports whether any error in err's tree matches target.
|
||||
func Is(x, y error) bool { return stderrors.Is(x, y) }
|
||||
|
||||
// Unwrap returns the result of calling the Unwrap method on err, if err's type contains
|
||||
// an Unwrap method returning error. Otherwise, Unwrap returns nil.
|
||||
//
|
||||
// Unwrap only calls a method of the form "Unwrap() error". In particular Unwrap does not
|
||||
// unwrap errors returned by [Join].
|
||||
func Unwrap(err error) error { return stderrors.Unwrap(err) }
|
||||
|
||||
// CombineErrors combines multiple errors into a single error.
|
||||
func CombineErrors(errors ...error) error {
|
||||
var combinedErrorMsg string
|
||||
|
||||
for _, err := range errors {
|
||||
if err != nil {
|
||||
if combinedErrorMsg != "" {
|
||||
combinedErrorMsg += "; " // Separate error messages with a delimiter
|
||||
}
|
||||
combinedErrorMsg += err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
if combinedErrorMsg == "" {
|
||||
return nil // No errors, return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("multiple errors occurred: [%s]", combinedErrorMsg)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue