-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
64 lines (54 loc) · 1.64 KB
/
errors.go
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
56
57
58
59
60
61
62
63
64
// Package errors provides error management.
//
// By convention, wrapping functions return a nil error if the given error is nil.
package errors
import (
std_errors "errors"
"github.com/pierrre/errors/errbase"
"github.com/pierrre/errors/errmsg"
"github.com/pierrre/errors/errstack"
)
// New returns a new error with a message and a stack.
func New(msg string) error {
err := errbase.New(msg)
err = errstack.WrapSkip(err, 1)
return err //nolint: wrapcheck // The error is wrapped.
}
// Newf returns a new error with a formatted message and a stack.
//
// It supports the %w verb.
func Newf(format string, args ...any) error {
err := errbase.Newf(format, args...)
err = errstack.WrapSkip(err, 1)
return err //nolint: wrapcheck // The error is wrapped.
}
// Wrap adds a message to an error, and a stack if it doesn't have one.
func Wrap(err error, msg string) error {
err = errstack.EnsureSkip(err, 1)
err = errmsg.Wrap(err, msg)
return err
}
// Wrapf adds a formatted message to an error, and a stack if it doesn't have one.
//
// It doesn't support the %w verb.
func Wrapf(err error, format string, args ...any) error {
err = errstack.EnsureSkip(err, 1)
err = errmsg.Wrapf(err, format, args...)
return err
}
// As is a wrapper for [std_errors.As].
func As(err error, target any) bool {
return std_errors.As(err, target)
}
// Is is a wrapper for [std_errors.Is].
func Is(err, target error) bool {
return std_errors.Is(err, target)
}
// Join is a wrapper for [std_errors.Join].
func Join(errs ...error) error {
return std_errors.Join(errs...)
}
// Unwrap is a wrapper for [std_errors.Unwrap].
func Unwrap(err error) error {
return std_errors.Unwrap(err)
}