File size: 929 Bytes
fea99b3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package errorsx
import (
"errors"
"fmt"
)
// WithPrefix annotates an error with a prefix.
func WithPrefix(err error, prefix string) error {
if err == nil {
return nil
}
type unwrapper interface {
Unwrap() []error
}
// Deliberately checking for the unwrapper interface instead of the errors.Is function.
// We only want to check the top-level error otherwise we may accidentally drop other wrappers from the error chain.
e, ok := err.(unwrapper)
if !ok {
return fmt.Errorf("%s: %w", prefix, err)
}
errs := e.Unwrap()
for i, err := range errs {
errs[i] = WithPrefix(err, prefix)
}
return errors.Join(errs...)
}
var _ error = (*warnError)(nil)
type warnError struct {
Err error
}
func (w *warnError) Error() string {
return w.Err.Error()
}
func (w *warnError) Unwrap() error {
return w.Err
}
func NewWarnError(err error) error {
if err == nil {
return nil
}
return &warnError{Err: err}
}
|