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 {
103103 return false
104104}
105105
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+
106114// Type returns the exact type of this error.
107115// With transparent wrapping, such as in Decorate(), returns the type of the original cause.
108116// 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 22
33package errorx
44
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 {
96 e := Cast (err )
107 return e != nil && e .IsOfType (t )
118}
129
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 {
1711 cause := e
1812 for cause != nil {
1913 if ! cause .transparent {
Original file line number Diff line number Diff line change @@ -4,20 +4,12 @@ package errorx
44
55import "errors"
66
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 {
118 e := burrowForTyped (err )
129 return e != nil && e .IsOfType (t )
1310}
1411
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 {
2113 cause := e
2214 for cause != nil {
2315 if ! cause .transparent {
@@ -44,5 +36,3 @@ func burrowForTyped(err error) *Error {
4436
4537 return nil
4638}
47-
48-
Original file line number Diff line number Diff line change @@ -122,4 +122,10 @@ func TestErrorsAndErrorx(t *testing.T) {
122122 err := fmt .Errorf ("error test: %w" , Decorate (io .EOF , "test" ))
123123 require .True (t , errors .Is (err , io .EOF ))
124124 })
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+ })
125131}
Original file line number Diff line number Diff line change @@ -95,6 +95,14 @@ func (t *Type) HasTrait(key Trait) bool {
9595 return ok
9696}
9797
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+
98106// Supertype returns a parent type, if present.
99107func (t * Type ) Supertype () * Type {
100108 return t .parent
You can’t perform that action at this time.
0 commit comments