9
9
10
10
type unwrap interface { Unwrap () error }
11
11
type is interface { Is (error ) bool }
12
- type as interface { As (interface {} ) bool }
12
+ type as interface { As (any ) bool }
13
13
14
14
// ErrorFormatFn represents the error formatting function for a Chain of errors.
15
15
type ErrorFormatFn func (Chain ) string
@@ -42,8 +42,8 @@ func New(s string) Chain {
42
42
}
43
43
44
44
// Newf creates an error with the provided text and automatically wraps it with line information.
45
- // it also accepts a varadic for optional message formatting.
46
- func Newf (format string , a ... interface {} ) Chain {
45
+ // it also accepts a variadic for optional message formatting.
46
+ func Newf (format string , a ... any ) Chain {
47
47
return wrap (fmt .Errorf (format , a ... ), "" , 3 )
48
48
}
49
49
@@ -55,8 +55,8 @@ func Wrap(err error, prefix string) Chain {
55
55
56
56
// Wrapf encapsulates the error, stores a contextual prefix and automatically obtains
57
57
// a stack trace.
58
- // it also accepts a varadic for prefix formatting.
59
- func Wrapf (err error , prefix string , a ... interface {} ) Chain {
58
+ // it also accepts a variadic for prefix formatting.
59
+ func Wrapf (err error , prefix string , a ... any ) Chain {
60
60
return wrap (err , fmt .Sprintf (prefix , a ... ), 3 )
61
61
}
62
62
@@ -67,6 +67,9 @@ func WrapSkipFrames(err error, prefix string, n uint) Chain {
67
67
}
68
68
69
69
func wrap (err error , prefix string , skipFrames int ) (c Chain ) {
70
+ if err == nil {
71
+ panic ("errors: Wrap|Wrapf called with nil error" )
72
+ }
70
73
var ok bool
71
74
if c , ok = err .(Chain ); ok {
72
75
c = append (c , newLink (err , prefix , skipFrames ))
@@ -81,7 +84,7 @@ func wrap(err error, prefix string, skipFrames int) (c Chain) {
81
84
return
82
85
}
83
86
84
- // Cause extracts and returns the root wrapped error (the naked error with no additional information
87
+ // Cause extracts and returns the root wrapped error (the naked error with no additional information)
85
88
func Cause (err error ) error {
86
89
for {
87
90
switch t := err .(type ) {
@@ -121,8 +124,8 @@ func HasType(err error, typ string) bool {
121
124
}
122
125
}
123
126
124
- // LookupTag recursively searches for the provided tag and returns it's value or nil
125
- func LookupTag (err error , key string ) interface {} {
127
+ // LookupTag recursively searches for the provided tag and returns its value or nil
128
+ func LookupTag (err error , key string ) any {
126
129
for {
127
130
switch t := err .(type ) {
128
131
case Chain :
@@ -143,17 +146,17 @@ func LookupTag(err error, key string) interface{} {
143
146
}
144
147
}
145
148
146
- // Is is to allow this library to be a drop-in replacement to the std library.
149
+ // Is allows this library to be a drop-in replacement to the std library.
147
150
//
148
- // Is reports whether any error in err's chain matches target.
151
+ // Is reports whether any error in the error chain matches target.
149
152
//
150
153
// The chain consists of err itself followed by the sequence of errors obtained by
151
154
// repeatedly calling Unwrap.
152
155
//
153
156
// An error is considered to match a target if it is equal to that target or if
154
157
// it implements a method Is(error) bool such that Is(target) returns true.
155
158
//
156
- // An error type might provide an Is method so it can be treated as equivalent
159
+ // An error type might provide an Is method, so it can be treated as equivalent
157
160
// to an existing error. For example, if MyError defines
158
161
//
159
162
// func (m MyError) Is(target error) bool { return target == os.ErrExist }
@@ -166,22 +169,22 @@ func Is(err, target error) bool {
166
169
167
170
// As is to allow this library to be a drop-in replacement to the std library.
168
171
//
169
- // As finds the first error in err's chain that matches target, and if so, sets
172
+ // As finds the first error in the error chain that matches target, and if so, sets
170
173
// target to that error value and returns true. Otherwise, it returns false.
171
174
//
172
175
// The chain consists of err itself followed by the sequence of errors obtained by
173
176
// repeatedly calling Unwrap.
174
177
//
175
178
// An error matches target if the error's concrete value is assignable to the value
176
- // pointed to by target, or if the error has a method As(interface{} ) bool such that
179
+ // pointed to by target, or if the error has a method As(any ) bool such that
177
180
// As(target) returns true. In the latter case, the As method is responsible for
178
181
// setting target.
179
182
//
180
- // An error type might provide an As method so it can be treated as if it were a
183
+ // An error type might provide an As method, so it can be treated as if it were
181
184
// a different error type.
182
185
//
183
186
// As panics if target is not a non-nil pointer to either a type that implements
184
187
// error, or to any interface type.
185
- func As (err error , target interface {} ) bool {
186
- return stderrors .As (err , target )
188
+ func As (err error , target any ) bool {
189
+ return stderrors .As (err , & target )
187
190
}
0 commit comments