File tree Expand file tree Collapse file tree 5 files changed +26
-20
lines changed Expand file tree Collapse file tree 5 files changed +26
-20
lines changed Original file line number Diff line number Diff line change @@ -103,6 +103,14 @@ func (e *Error) HasTrait(key Trait) bool {
103
103
return false
104
104
}
105
105
106
+ // IsOfType is a proper type check for an errorx-based errors.
107
+ // It takes the transparency and error types hierarchy into account,
108
+ // so that type check against any supertype of the original cause passes.
109
+ // Go 1.13 and above: it also tolerates non-errorx errors in chain if those errors support errors unwrap.
110
+ func (e * Error ) IsOfType (t * Type ) bool {
111
+ return e .isOfType (t )
112
+ }
113
+
106
114
// Type returns the exact type of this error.
107
115
// With transparent wrapping, such as in Decorate(), returns the type of the original cause.
108
116
// The result is always not nil, even if the resulting type is impossible to successfully type check against.
Original file line number Diff line number Diff line change 2
2
3
3
package errorx
4
4
5
- // IsOfType is a type check for errors.
6
- // Returns true either if both are of exactly the same type, or if the same is true for one of current type's ancestors.
7
- // For an error that does not have an errorx type, returns false.
8
- func IsOfType (err error , t * Type ) bool {
5
+ func isOfType (err error , t * Type ) bool {
9
6
e := Cast (err )
10
7
return e != nil && e .IsOfType (t )
11
8
}
12
9
13
- // IsOfType is a proper type check for an errorx-based errors.
14
- // It takes the transparency and error types hierarchy into account,
15
- // so that type check against any supertype of the original cause passes.
16
- func (e * Error ) IsOfType (t * Type ) bool {
10
+ func (e * Error ) isOfType (t * Type ) bool {
17
11
cause := e
18
12
for cause != nil {
19
13
if ! cause .transparent {
Original file line number Diff line number Diff line change @@ -4,20 +4,12 @@ package errorx
4
4
5
5
import "errors"
6
6
7
- // IsOfType is a type check for errors.
8
- // Returns true either if both are of exactly the same type, or if the same is true for one of current type's ancestors.
9
- // For an error that does not have an errorx type, returns false unless it wraps another error of errorx type.
10
- func IsOfType (err error , t * Type ) bool {
7
+ func isOfType (err error , t * Type ) bool {
11
8
e := burrowForTyped (err )
12
9
return e != nil && e .IsOfType (t )
13
10
}
14
11
15
-
16
- // IsOfType is a proper type check for an errorx-based errors.
17
- // It takes the transparency and error types hierarchy into account,
18
- // so that type check against any supertype of the original cause passes.
19
- // It also tolerates non-errorx errors in chain if those errors support go 1.13 errors unwrap.
20
- func (e * Error ) IsOfType (t * Type ) bool {
12
+ func (e * Error ) isOfType (t * Type ) bool {
21
13
cause := e
22
14
for cause != nil {
23
15
if ! cause .transparent {
@@ -44,5 +36,3 @@ func burrowForTyped(err error) *Error {
44
36
45
37
return nil
46
38
}
47
-
48
-
Original file line number Diff line number Diff line change @@ -122,4 +122,10 @@ func TestErrorsAndErrorx(t *testing.T) {
122
122
err := fmt .Errorf ("error test: %w" , Decorate (io .EOF , "test" ))
123
123
require .True (t , errors .Is (err , io .EOF ))
124
124
})
125
+
126
+ t .Run ("Wrap" , func (t * testing.T ) {
127
+ err := fmt .Errorf ("error test: %w" , testType .Wrap (io .EOF , "test" ))
128
+ require .False (t , errors .Is (err , io .EOF ))
129
+ require .True (t , errors .Is (err , testType .NewWithNoMessage ()))
130
+ })
125
131
}
Original file line number Diff line number Diff line change @@ -95,6 +95,14 @@ func (t *Type) HasTrait(key Trait) bool {
95
95
return ok
96
96
}
97
97
98
+ // IsOfType is a type check for errors.
99
+ // Returns true either if both are of exactly the same type, or if the same is true for one of current type's ancestors.
100
+ // Go 1.12 and below: for an error that does not have an errorx type, returns false.
101
+ // Go 1.13 and above: for an error that does not have an errorx type, returns false unless it wraps another error of errorx type.
102
+ func IsOfType (err error , t * Type ) bool {
103
+ return isOfType (err , t )
104
+ }
105
+
98
106
// Supertype returns a parent type, if present.
99
107
func (t * Type ) Supertype () * Type {
100
108
return t .parent
You can’t perform that action at this time.
0 commit comments