You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
778 B

  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. //go:build !go1.13
  5. // +build !go1.13
  6. package errors
  7. import "reflect"
  8. // Is is a copy of Go 1.13's errors.Is for use with older Go versions.
  9. func Is(err, target error) bool {
  10. if target == nil {
  11. return err == target
  12. }
  13. isComparable := reflect.TypeOf(target).Comparable()
  14. for {
  15. if isComparable && err == target {
  16. return true
  17. }
  18. if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
  19. return true
  20. }
  21. if err = unwrap(err); err == nil {
  22. return false
  23. }
  24. }
  25. }
  26. func unwrap(err error) error {
  27. u, ok := err.(interface {
  28. Unwrap() error
  29. })
  30. if !ok {
  31. return nil
  32. }
  33. return u.Unwrap()
  34. }