Skip to content

Commit

Permalink
test: use native errors.As after upgrading to Go1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
jszwec committed Feb 14, 2023
1 parent 21889c0 commit 63db5fb
Showing 1 changed file with 2 additions and 45 deletions.
47 changes: 2 additions & 45 deletions csvutil_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package csvutil

import (
"errors"
"reflect"
"testing"
)
Expand Down Expand Up @@ -879,52 +880,8 @@ func checkErr(expected, err error) bool {
}

eVal := reflect.New(reflect.TypeOf(expected))
if !asError(err, eVal.Interface()) {
if !errors.As(err, eVal.Interface()) {
return false
}
return reflect.DeepEqual(eVal.Elem().Interface(), expected)
}

// asError is a copy of errors.As to support older Go versions.
//
// This copy exists because we want to avoid dependencies like:
// "golang.org/x/xerrors"
func asError(err error, target any) bool {
if target == nil {
panic("errors: target cannot be nil")
}
val := reflect.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflect.Ptr || val.IsNil() {
panic("errors: target must be a non-nil pointer")
}
targetType := typ.Elem()
if targetType.Kind() != reflect.Interface && !targetType.Implements(errorType) {
panic("errors: *target must be interface or implement error")
}
for err != nil {
if reflect.TypeOf(err).AssignableTo(targetType) {
val.Elem().Set(reflect.ValueOf(err))
return true
}
if x, ok := err.(interface{ As(any) bool }); ok && x.As(target) {
return true
}
err = unwrap(err)
}
return false
}

var errorType = reflect.TypeOf((*error)(nil)).Elem()

// unwrap is a copy of errors.Unwrap for older Go versions and to avoid
// dependencies.
func unwrap(err error) error {
u, ok := err.(interface {
Unwrap() error
})
if !ok {
return nil
}
return u.Unwrap()
}

0 comments on commit 63db5fb

Please sign in to comment.