is_go112.go 759 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2020 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !go1.13
  5. package errors
  6. import "reflect"
  7. // Is is a copy of Go 1.13's errors.Is for use with older Go versions.
  8. func Is(err, target error) bool {
  9. if target == nil {
  10. return err == target
  11. }
  12. isComparable := reflect.TypeOf(target).Comparable()
  13. for {
  14. if isComparable && err == target {
  15. return true
  16. }
  17. if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
  18. return true
  19. }
  20. if err = unwrap(err); err == nil {
  21. return false
  22. }
  23. }
  24. }
  25. func unwrap(err error) error {
  26. u, ok := err.(interface {
  27. Unwrap() error
  28. })
  29. if !ok {
  30. return nil
  31. }
  32. return u.Unwrap()
  33. }