summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-08 13:27:37 +0800
committerKeuin <[email protected]>2022-09-08 13:27:37 +0800
commit177ecb3de2f9ca8e23c3eeb19b6875a0c6593355 (patch)
tree6a8d82c994317420f2294ac3678c497d596cc1fb /common
parentea58d5d3c9f0d4534a9ffd028b9461a75922d54f (diff)
Support specify IP network type (ipv4, ipv6, in arbitrary combination and priority)
Diffstat (limited to 'common')
-rw-r--r--common/errors.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/common/errors.go b/common/errors.go
new file mode 100644
index 0000000..ba686f1
--- /dev/null
+++ b/common/errors.go
@@ -0,0 +1,29 @@
+package common
+
+import (
+ "errors"
+ "reflect"
+)
+
+// IsErrorOfType is a modified version of errors.Is, which loosen the check condition
+func IsErrorOfType(err, target error) bool {
+ if target == nil {
+ return err == target
+ }
+
+ isComparable := reflect.TypeOf(target).Comparable()
+ for {
+ if isComparable && reflect.TypeOf(target) == reflect.TypeOf(err) {
+ return true
+ }
+ if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
+ return true
+ }
+ // TODO: consider supporting target.Is(err). This would allow
+ // user-definable predicates, but also may allow for coping with sloppy
+ // APIs, thereby making it easier to get away with them.
+ if err = errors.Unwrap(err); err == nil {
+ return false
+ }
+ }
+}