@@ -129,6 +129,50 @@ func TestIs(t *testing.T) {
129129 t .Errorf ("io.EOF is fmt.Errorf" )
130130 }
131131
132+ custErr := errorWithCustomIs {
133+ Key : "TestForFun" ,
134+ Err : io .EOF ,
135+ }
136+
137+ shouldMatch := errorWithCustomIs {
138+ Key : "TestForFun" ,
139+ }
140+
141+ shouldNotMatch := errorWithCustomIs {Key : "notOk" }
142+
143+ if ! Is (custErr , shouldMatch ) {
144+ t .Errorf ("custErr is not a TestForFun customError" )
145+ }
146+
147+ if Is (custErr , shouldNotMatch ) {
148+ t .Errorf ("custErr is a notOk customError" )
149+ }
150+
151+ if ! Is (custErr , New (shouldMatch )) {
152+ t .Errorf ("custErr is not a New(TestForFun customError)" )
153+ }
154+
155+ if Is (custErr , New (shouldNotMatch )) {
156+ t .Errorf ("custErr is a New(notOk customError)" )
157+ }
158+
159+ if ! Is (New (custErr ), shouldMatch ) {
160+ t .Errorf ("New(custErr) is not a TestForFun customError" )
161+ }
162+
163+ if Is (New (custErr ), shouldNotMatch ) {
164+ t .Errorf ("New(custErr) is a notOk customError" )
165+ }
166+
167+ if ! Is (New (custErr ), New (shouldMatch )) {
168+ t .Errorf ("New(custErr) is not a New(TestForFun customError)" )
169+ }
170+
171+ if Is (New (custErr ), New (shouldNotMatch )) {
172+ t .Errorf ("New(custErr) is a New(notOk customError)" )
173+ }
174+
175+
132176}
133177
134178func TestWrapError (t * testing.T ) {
@@ -317,3 +361,17 @@ func callersToFrames(callers []uintptr) []runtime.Frame {
317361 }
318362 }
319363}
364+
365+ type errorWithCustomIs struct {
366+ Key string
367+ Err error
368+ }
369+
370+ func (ewci errorWithCustomIs ) Error () string {
371+ return "[" + ewci .Key + "]: " + ewci .Err .Error ()
372+ }
373+
374+ func (ewci errorWithCustomIs ) Is (target error ) bool {
375+ matched , ok := target .(errorWithCustomIs )
376+ return ok && matched .Key == ewci .Key
377+ }
0 commit comments