99
1010type unwrap interface { Unwrap () error }
1111type is interface { Is (error ) bool }
12- type as interface { As (interface {} ) bool }
12+ type as interface { As (any ) bool }
1313
1414// ErrorFormatFn represents the error formatting function for a Chain of errors.
1515type ErrorFormatFn func (Chain ) string
@@ -42,8 +42,8 @@ func New(s string) Chain {
4242}
4343
4444// 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 {
4747 return wrap (fmt .Errorf (format , a ... ), "" , 3 )
4848}
4949
@@ -55,8 +55,8 @@ func Wrap(err error, prefix string) Chain {
5555
5656// Wrapf encapsulates the error, stores a contextual prefix and automatically obtains
5757// 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 {
6060 return wrap (err , fmt .Sprintf (prefix , a ... ), 3 )
6161}
6262
@@ -67,6 +67,9 @@ func WrapSkipFrames(err error, prefix string, n uint) Chain {
6767}
6868
6969func wrap (err error , prefix string , skipFrames int ) (c Chain ) {
70+ if err == nil {
71+ panic ("errors: Wrap|Wrapf called with nil error" )
72+ }
7073 var ok bool
7174 if c , ok = err .(Chain ); ok {
7275 c = append (c , newLink (err , prefix , skipFrames ))
@@ -81,7 +84,7 @@ func wrap(err error, prefix string, skipFrames int) (c Chain) {
8184 return
8285}
8386
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)
8588func Cause (err error ) error {
8689 for {
8790 switch t := err .(type ) {
@@ -121,8 +124,8 @@ func HasType(err error, typ string) bool {
121124 }
122125}
123126
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 {
126129 for {
127130 switch t := err .(type ) {
128131 case Chain :
@@ -143,17 +146,17 @@ func LookupTag(err error, key string) interface{} {
143146 }
144147}
145148
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.
147150//
148- // Is reports whether any error in err's chain matches target.
151+ // Is reports whether any error in the error chain matches target.
149152//
150153// The chain consists of err itself followed by the sequence of errors obtained by
151154// repeatedly calling Unwrap.
152155//
153156// An error is considered to match a target if it is equal to that target or if
154157// it implements a method Is(error) bool such that Is(target) returns true.
155158//
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
157160// to an existing error. For example, if MyError defines
158161//
159162// func (m MyError) Is(target error) bool { return target == os.ErrExist }
@@ -166,22 +169,22 @@ func Is(err, target error) bool {
166169
167170// As is to allow this library to be a drop-in replacement to the std library.
168171//
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
170173// target to that error value and returns true. Otherwise, it returns false.
171174//
172175// The chain consists of err itself followed by the sequence of errors obtained by
173176// repeatedly calling Unwrap.
174177//
175178// 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
177180// As(target) returns true. In the latter case, the As method is responsible for
178181// setting target.
179182//
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
181184// a different error type.
182185//
183186// As panics if target is not a non-nil pointer to either a type that implements
184187// 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 )
187190}
0 commit comments