@@ -403,11 +403,9 @@ func (*functionSuite) TestQuietWrappedErrorStillSatisfied(c *gc.C) {
403403 c .Assert (errors .Is (err , simpleTestError ), gc .Equals , true )
404404}
405405
406- type FooError struct {
407- }
408-
409- func (* FooError ) Error () string {
410- return "I am here boss"
406+ type ComplexErrorMessage interface {
407+ error
408+ ComplexMessage () string
411409}
412410
413411type complexError struct {
@@ -418,30 +416,90 @@ func (c *complexError) Error() string {
418416 return c .Message
419417}
420418
419+ func (c * complexError ) ComplexMessage () string {
420+ return c .Message
421+ }
422+
421423type complexErrorOther struct {
422424 Message string
423425}
424426
427+ func (c * complexErrorOther ) As (e any ) bool {
428+ if ce , ok := e .(* * complexError ); ok {
429+ * ce = & complexError {
430+ Message : c .Message ,
431+ }
432+ return true
433+ }
434+ return false
435+ }
436+
425437func (c * complexErrorOther ) Error () string {
426438 return c .Message
427439}
428440
429- func (* functionSuite ) TestIsType (c * gc.C ) {
441+ func (c * complexErrorOther ) ComplexMessage () string {
442+ return c .Message
443+ }
444+
445+ func (* functionSuite ) TestHasType (c * gc.C ) {
430446 complexErr := & complexError {Message : "complex error message" }
431447 wrapped1 := fmt .Errorf ("wrapping1: %w" , complexErr )
432448 wrapped2 := fmt .Errorf ("wrapping2: %w" , wrapped1 )
433449
434- c .Assert (errors.IsType [* complexError ](complexErr ), gc .Equals , true )
435- c .Assert (errors.IsType [* complexError ](wrapped1 ), gc .Equals , true )
436- c .Assert (errors.IsType [* complexError ](wrapped2 ), gc .Equals , true )
437- c .Assert (errors.IsType [ * complexErrorOther ]( complexErr ), gc .Equals , false )
438- c .Assert (errors.IsType [* complexErrorOther ](wrapped1 ), gc .Equals , false )
439- c .Assert (errors.IsType [* complexErrorOther ](wrapped2 ), gc .Equals , false )
450+ c .Assert (errors.HasType [* complexError ](complexErr ), gc .Equals , true )
451+ c .Assert (errors.HasType [* complexError ](wrapped1 ), gc .Equals , true )
452+ c .Assert (errors.HasType [* complexError ](wrapped2 ), gc .Equals , true )
453+ c .Assert (errors.HasType [ ComplexErrorMessage ]( wrapped2 ), gc .Equals , true )
454+ c .Assert (errors.HasType [* complexErrorOther ](wrapped2 ), gc .Equals , false )
455+ c .Assert (errors.HasType [* complexErrorOther ](nil ), gc .Equals , false )
440456
441- err := errors .New ("test" )
442- c .Assert (errors.IsType [* complexErrorOther ](err ), gc .Equals , false )
457+ complexErrOther := & complexErrorOther {Message : "another complex error" }
458+
459+ c .Assert (errors.HasType [* complexError ](complexErrOther ), gc .Equals , true )
443460
444- c .Assert (errors.IsType [* complexErrorOther ](nil ), gc .Equals , false )
461+ wrapped2 = fmt .Errorf ("wrapping1: %w" , complexErrOther )
462+ c .Assert (errors.HasType [* complexError ](wrapped2 ), gc .Equals , true )
463+ }
464+
465+ func (* functionSuite ) TestAsType (c * gc.C ) {
466+ complexErr := & complexError {Message : "complex error message" }
467+ wrapped1 := fmt .Errorf ("wrapping1: %w" , complexErr )
468+ wrapped2 := fmt .Errorf ("wrapping2: %w" , wrapped1 )
469+
470+ ce , ok := errors.AsType [* complexError ](complexErr )
471+ c .Assert (ok , gc .Equals , true )
472+ c .Assert (ce .Message , gc .Equals , complexErr .Message )
473+
474+ ce , ok = errors.AsType [* complexError ](wrapped1 )
475+ c .Assert (ok , gc .Equals , true )
476+ c .Assert (ce .Message , gc .Equals , complexErr .Message )
477+
478+ ce , ok = errors.AsType [* complexError ](wrapped2 )
479+ c .Assert (ok , gc .Equals , true )
480+ c .Assert (ce .Message , gc .Equals , complexErr .Message )
481+
482+ cem , ok := errors.AsType [ComplexErrorMessage ](wrapped2 )
483+ c .Assert (ok , gc .Equals , true )
484+ c .Assert (cem .ComplexMessage (), gc .Equals , complexErr .Message )
485+
486+ ceo , ok := errors.AsType [* complexErrorOther ](wrapped2 )
487+ c .Assert (ok , gc .Equals , false )
488+ c .Assert (ceo , gc .Equals , (* complexErrorOther )(nil ))
489+
490+ ceo , ok = errors.AsType [* complexErrorOther ](nil )
491+ c .Assert (ok , gc .Equals , false )
492+ c .Assert (ceo , gc .Equals , (* complexErrorOther )(nil ))
493+
494+ complexErrOther := & complexErrorOther {Message : "another complex error" }
495+ ce , ok = errors.AsType [* complexError ](complexErrOther )
496+ c .Assert (ok , gc .Equals , true )
497+ c .Assert (ce .Message , gc .Equals , complexErrOther .Message )
498+
499+ wrapped2 = fmt .Errorf ("wrapping1: %w" , complexErrOther )
500+ ce , ok = errors.AsType [* complexError ](wrapped2 )
501+ c .Assert (ok , gc .Equals , true )
502+ c .Assert (ce .Message , gc .Equals , complexErrOther .Message )
445503}
446504
447505func ExampleHide () {
@@ -464,12 +522,24 @@ func (m *MyError) Error() string {
464522 return m .Message
465523}
466524
467- func ExampleIsType () {
525+ func ExampleHasType () {
526+ myErr := & MyError {Message : "these are not the droids you're looking for" }
527+ err := fmt .Errorf ("wrapped: %w" , myErr )
528+ is := errors.HasType [* MyError ](err )
529+ fmt .Println (is )
530+
531+ // Output:
532+ // true
533+ }
534+
535+ func ExampleAsType () {
468536 myErr := & MyError {Message : "these are not the droids you're looking for" }
469537 err := fmt .Errorf ("wrapped: %w" , myErr )
470- is := errors.IsType [* MyError ](err )
538+ myErr , is := errors.AsType [* MyError ](err )
471539 fmt .Println (is )
540+ fmt .Println (myErr .Message )
472541
473542 // Output:
474543 // true
544+ // these are not the droids you're looking for
475545}
0 commit comments