blob: fbcd349207dd04440db1340c480c42fd8c464b08 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001// 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
Joey Armstrongba3d9d12024-01-15 14:22:11 -05005//go:build !go1.13
khenaidoo5fc5cea2021-08-11 17:39:16 -04006// +build !go1.13
7
8package errors
9
10import "reflect"
11
12// Is is a copy of Go 1.13's errors.Is for use with older Go versions.
13func Is(err, target error) bool {
14 if target == nil {
15 return err == target
16 }
17
18 isComparable := reflect.TypeOf(target).Comparable()
19 for {
20 if isComparable && err == target {
21 return true
22 }
23 if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
24 return true
25 }
26 if err = unwrap(err); err == nil {
27 return false
28 }
29 }
30}
31
32func unwrap(err error) error {
33 u, ok := err.(interface {
34 Unwrap() error
35 })
36 if !ok {
37 return nil
38 }
39 return u.Unwrap()
40}