2022-05-08 17:49:45 +00:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
2023-01-17 11:25:13 +00:00
|
|
|
_ "unsafe"
|
2022-05-08 17:49:45 +00:00
|
|
|
|
|
|
|
"codeberg.org/gruf/go-bitutil"
|
|
|
|
)
|
|
|
|
|
2023-04-29 16:44:20 +00:00
|
|
|
// errtype is a ptr to the error interface type.
|
|
|
|
var errtype = reflect.TypeOf((*error)(nil)).Elem()
|
|
|
|
|
|
|
|
// Comparable is functionally equivalent to calling errors.Is() on multiple errors (up to a max of 64).
|
|
|
|
func Comparable(err error, targets ...error) bool {
|
2022-05-08 17:49:45 +00:00
|
|
|
var flags bitutil.Flags64
|
|
|
|
|
2023-01-17 11:25:13 +00:00
|
|
|
// Flags only has 64 bit-slots
|
2022-05-08 17:49:45 +00:00
|
|
|
if len(targets) > 64 {
|
|
|
|
panic("too many targets")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(targets); {
|
|
|
|
if targets[i] == nil {
|
2023-04-29 16:44:20 +00:00
|
|
|
if err == nil {
|
2022-07-10 15:18:21 +00:00
|
|
|
return true
|
|
|
|
}
|
2023-04-29 16:44:20 +00:00
|
|
|
|
|
|
|
// Drop nil targets from slice.
|
|
|
|
copy(targets[i:], targets[i+1:])
|
|
|
|
targets = targets[:len(targets)-1]
|
2022-05-08 17:49:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this error is directly comparable
|
|
|
|
if reflect.TypeOf(targets[i]).Comparable() {
|
|
|
|
flags = flags.Set(uint8(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
for err != nil {
|
|
|
|
// Check if this layer supports .Is interface
|
|
|
|
is, ok := err.(interface{ Is(error) bool })
|
|
|
|
|
2023-01-17 11:25:13 +00:00
|
|
|
if !ok {
|
|
|
|
// Error does not support interface
|
|
|
|
//
|
|
|
|
// Only try perform direct compare
|
|
|
|
for i := 0; i < len(targets); i++ {
|
|
|
|
// Try directly compare errors
|
|
|
|
if flags.Get(uint8(i)) &&
|
|
|
|
err == targets[i] {
|
|
|
|
return true
|
|
|
|
}
|
2022-05-08 17:49:45 +00:00
|
|
|
}
|
2023-01-17 11:25:13 +00:00
|
|
|
} else {
|
|
|
|
// Error supports the .Is interface
|
|
|
|
//
|
|
|
|
// Perform direct compare AND .Is()
|
|
|
|
for i := 0; i < len(targets); i++ {
|
|
|
|
if (flags.Get(uint8(i)) &&
|
|
|
|
err == targets[i]) ||
|
|
|
|
is.Is(targets[i]) {
|
|
|
|
return true
|
|
|
|
}
|
2022-05-08 17:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap to next layer
|
|
|
|
err = errors.Unwrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-29 16:44:20 +00:00
|
|
|
// Assignable is functionally equivalent to calling errors.As() on multiple errors,
|
|
|
|
// except that it only checks assignability as opposed to setting the target.
|
|
|
|
func Assignable(err error, targets ...error) bool {
|
|
|
|
if err == nil {
|
|
|
|
// Fastest case.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(targets); {
|
|
|
|
if targets[i] == nil {
|
|
|
|
// Drop nil targets from slice.
|
|
|
|
copy(targets[i:], targets[i+1:])
|
|
|
|
targets = targets[:len(targets)-1]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
for err != nil {
|
|
|
|
// Check if this layer supports .As interface
|
|
|
|
as, ok := err.(interface{ As(any) bool })
|
|
|
|
|
|
|
|
// Get reflected err type.
|
|
|
|
te := reflect.TypeOf(err)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// Error does not support interface.
|
|
|
|
//
|
|
|
|
// Check assignability using reflection.
|
|
|
|
for i := 0; i < len(targets); i++ {
|
|
|
|
tt := reflect.TypeOf(targets[i])
|
|
|
|
if te.AssignableTo(tt) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Error supports the .As interface.
|
|
|
|
//
|
|
|
|
// Check using .As() and reflection.
|
|
|
|
for i := 0; i < len(targets); i++ {
|
|
|
|
if as.As(targets[i]) {
|
|
|
|
return true
|
|
|
|
} else if tt := reflect.TypeOf(targets[i]); // nocollapse
|
|
|
|
te.AssignableTo(tt) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap to next layer.
|
|
|
|
err = errors.Unwrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// As finds the first error in err's tree that matches target, and if one is found, sets
|
2022-05-08 17:49:45 +00:00
|
|
|
// target to that error value and returns true. Otherwise, it returns false.
|
|
|
|
//
|
2023-04-29 16:44:20 +00:00
|
|
|
// The tree consists of err itself, followed by the errors obtained by repeatedly
|
|
|
|
// calling Unwrap. When err wraps multiple errors, As examines err followed by a
|
|
|
|
// depth-first traversal of its children.
|
2022-05-08 17:49:45 +00:00
|
|
|
//
|
|
|
|
// An error matches target if the error's concrete value is assignable to the value
|
|
|
|
// pointed to by target, or if the error has a method As(interface{}) bool such that
|
|
|
|
// As(target) returns true. In the latter case, the As method is responsible for
|
|
|
|
// setting target.
|
|
|
|
//
|
|
|
|
// An error type might provide an As method so it can be treated as if it were a
|
|
|
|
// different error type.
|
|
|
|
//
|
|
|
|
// As panics if target is not a non-nil pointer to either a type that implements
|
|
|
|
// error, or to any interface type.
|
2023-01-17 11:25:13 +00:00
|
|
|
//
|
|
|
|
//go:linkname As errors.As
|
2023-04-29 16:44:20 +00:00
|
|
|
func As(err error, target any) bool
|
2022-05-08 17:49:45 +00:00
|
|
|
|
|
|
|
// Unwrap returns the result of calling the Unwrap method on err, if err's
|
|
|
|
// type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.
|
2023-01-17 11:25:13 +00:00
|
|
|
//
|
|
|
|
//go:linkname Unwrap errors.Unwrap
|
|
|
|
func Unwrap(err error) error
|