-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompatibility_pkg_errors.go
More file actions
107 lines (95 loc) · 3.52 KB
/
compatibility_pkg_errors.go
File metadata and controls
107 lines (95 loc) · 3.52 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package errors
import "github.com/frederik-jatzkowski/errors/internal"
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement the Cause() method, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
//
// This function allows compatibility for [github.com/pkg/errors.Cause] but is
// currently not encouraged by this module.
//
// Deprecated: Use the recommended API of this module instead. Use [Is]
// and [As] for error inspection.
func Cause(err error) error {
if err == nil {
return nil
}
if causer, ok := err.(interface{ Cause() error }); ok {
return causer.Cause()
}
return err
}
// WithMessage annotates err with a new message.
// If err is nil, [WithMessage] returns nil.
//
// This implementation uses the module's [Errorf] functionality internally,
// which means it will add stack traces unless the error already has one.
//
// This function allows compatibility for [github.com/pkg/errors.WithMessage] but is
// currently not encouraged by this module.
//
// Deprecated: Use the recommended API of this module instead. Use [Errorf]
// with %w verb for wrapping errors with messages.
func WithMessage(err error, msg string) error {
if err == nil {
return nil
}
return internal.Errorf(1, "%s: %w", msg, err)
}
// WithMessagef annotates err with the format specifier and arguments.
// If err is nil, [WithMessagef] returns nil.
//
// This implementation uses the module's [Errorf] functionality internally,
// which means it will add stack traces unless the error already has one.
//
// This function allows compatibility for [github.com/pkg/errors.WithMessagef] but is
// currently not encouraged by this module.
//
// Deprecated: Use the recommended API of this module instead. Use [Errorf]
// with %w verb for wrapping errors with formatted messages.
func WithMessagef(err error, format string, args ...any) error {
if err == nil {
return nil
}
return internal.Errorf(1, format+": %w", append(args, err)...)
}
// Wrap returns an error annotating err with a stack trace at the point [Wrap] is
// called, and the supplied message. If err is nil, [Wrap] returns nil.
//
// This implementation uses the module's [Errorf] functionality internally,
// which means it will add stack traces unless the error already has one.
//
// This function allows compatibility for [github.com/pkg/errors.Wrap] but is
// currently not encouraged by this module.
//
// Deprecated: Use the recommended API of this module instead. Use [Errorf]
// with %w verb: [Errorf]("message: %w", err).
func Wrap(err error, msg string) error {
if err == nil {
return nil
}
return internal.Errorf(1, "%s: %w", msg, err)
}
// Wrapf returns an error annotating err with a stack trace at the point [Wrapf] is
// called, and the format specifier. If err is nil, [Wrapf] returns nil.
//
// This implementation uses the module's [Errorf] functionality internally,
// which means it will add stack traces unless the error already has one.
//
// This function allows compatibility for [github.com/pkg/errors.Wrapf] but is
// currently not encouraged by this module.
//
// Deprecated: Use the recommended API of this module instead. Use [Errorf]
// with %w verb for wrapping errors with formatted messages.
func Wrapf(err error, format string, args ...any) error {
if err == nil {
return nil
}
return internal.Errorf(1, format+": %w", append(args, err)...)
}