2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-27 07:16:40 +00:00

Add CombineErrors helper function

This commit is contained in:
Aneesh Nireshwalia 2024-02-22 16:57:00 -07:00
parent c6311c1e32
commit 94de87d4b7
No known key found for this signature in database
GPG Key ID: 6F5A52831C046F44

View File

@ -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)
}