2016-09-01 20:17:37 +00:00
|
|
|
package errors
|
|
|
|
|
2018-01-23 20:40:51 +00:00
|
|
|
import (
|
2022-10-08 10:37:18 +00:00
|
|
|
stderrors "errors"
|
2024-02-22 23:57:00 +00:00
|
|
|
"fmt"
|
2016-09-01 20:17:37 +00:00
|
|
|
|
2018-01-23 20:40:51 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2016-09-01 20:17:37 +00:00
|
|
|
|
2016-09-18 11:28:59 +00:00
|
|
|
// New creates a new error based on message. Wrapped so that this package does
|
|
|
|
// not appear in the stack trace.
|
|
|
|
var New = errors.New
|
2016-09-01 20:17:37 +00:00
|
|
|
|
2016-09-18 11:28:59 +00:00
|
|
|
// Errorf creates an error based on a format string and values. Wrapped so that
|
|
|
|
// this package does not appear in the stack trace.
|
|
|
|
var Errorf = errors.Errorf
|
2016-09-01 20:17:37 +00:00
|
|
|
|
2016-09-18 11:28:59 +00:00
|
|
|
// Wrap wraps an error retrieved from outside of restic. Wrapped so that this
|
|
|
|
// package does not appear in the stack trace.
|
|
|
|
var Wrap = errors.Wrap
|
2017-02-11 13:22:04 +00:00
|
|
|
|
|
|
|
// Wrapf returns an error annotating err with the format specifier. If err is
|
|
|
|
// nil, Wrapf returns nil.
|
|
|
|
var Wrapf = errors.Wrapf
|
2018-01-23 20:40:51 +00:00
|
|
|
|
2024-02-22 23:57:00 +00:00
|
|
|
// WithStack annotates err with a stack trace at the point WithStack was called.
|
|
|
|
// If err is nil, WithStack returns nil.
|
2021-06-07 17:26:25 +00:00
|
|
|
var WithStack = errors.WithStack
|
|
|
|
|
2020-12-16 12:58:02 +00:00
|
|
|
// Go 1.13-style error handling.
|
|
|
|
|
2024-02-22 23:57:00 +00:00
|
|
|
// 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.
|
2022-10-08 10:37:18 +00:00
|
|
|
func As(err error, tgt interface{}) bool { return stderrors.As(err, tgt) }
|
|
|
|
|
2024-02-22 23:57:00 +00:00
|
|
|
// Is reports whether any error in err's tree matches target.
|
2022-10-08 10:37:18 +00:00
|
|
|
func Is(x, y error) bool { return stderrors.Is(x, y) }
|
2022-06-13 18:35:37 +00:00
|
|
|
|
2024-02-22 23:57:00 +00:00
|
|
|
// 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].
|
2022-10-08 10:37:18 +00:00
|
|
|
func Unwrap(err error) error { return stderrors.Unwrap(err) }
|
2024-02-22 23:57:00 +00:00
|
|
|
|
2024-02-24 20:22:34 +00:00
|
|
|
// CombineErrors combines multiple errors into a single error after filtering out any nil values.
|
|
|
|
// If no errors are passed, it returns nil.
|
|
|
|
// If one error is passed, it simply returns that same error.
|
|
|
|
func CombineErrors(errors ...error) (err error) {
|
2024-02-22 23:57:00 +00:00
|
|
|
var combinedErrorMsg string
|
2024-02-24 20:22:34 +00:00
|
|
|
var multipleErrors bool
|
|
|
|
for _, errVal := range errors {
|
|
|
|
if errVal != nil {
|
2024-02-22 23:57:00 +00:00
|
|
|
if combinedErrorMsg != "" {
|
|
|
|
combinedErrorMsg += "; " // Separate error messages with a delimiter
|
2024-02-24 20:22:34 +00:00
|
|
|
multipleErrors = true
|
|
|
|
} else {
|
|
|
|
// Set the first error
|
|
|
|
err = errVal
|
2024-02-22 23:57:00 +00:00
|
|
|
}
|
2024-02-24 20:22:34 +00:00
|
|
|
combinedErrorMsg += errVal.Error()
|
2024-02-22 23:57:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if combinedErrorMsg == "" {
|
2024-02-24 20:22:34 +00:00
|
|
|
return nil // If no errors, return nil
|
|
|
|
} else if !multipleErrors {
|
|
|
|
return err // If only one error, return that first error
|
2024-02-22 23:57:00 +00:00
|
|
|
}
|
2024-02-24 20:47:49 +00:00
|
|
|
return fmt.Errorf("multiple errors occurred: [%s]", combinedErrorMsg)
|
2024-02-22 23:57:00 +00:00
|
|
|
}
|