Skip to content

Commit

Permalink
Merge pull request #100 from multiformats/marco/fix-Is
Browse files Browse the repository at this point in the history
Fix errors Is checking
  • Loading branch information
MarcoPolo authored Feb 9, 2023
2 parents f731e9a + e1ea64f commit 9691dd5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func (e ErrNotSupported[T]) Error() string {
return fmt.Sprintf("protocols not supported: %v", e.Protos)
}

func (e ErrNotSupported[T]) Is(target error) bool {
_, ok := target.(ErrNotSupported[T])
return ok
}

// ErrNoProtocols is the error returned when the no protocols have been
// specified.
var ErrNoProtocols = errors.New("no protocols specified")
Expand Down
29 changes: 29 additions & 0 deletions multistream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package multistream
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -997,3 +998,31 @@ func FuzzMultistream(f *testing.F) {
_ = mux.Handle(input)
})
}

func TestComparableErrors(t *testing.T) {
var err1 error = ErrNotSupported[string]{[]string{"/a"}}
if !errors.Is(err1, ErrNotSupported[string]{}) {
t.Fatalf("Should be comparable")
}

err2 := fmt.Errorf("This is wrapped: %w", err1)
if !errors.Is(err2, ErrNotSupported[string]{}) {
t.Fatalf("Should be comparable")
}

type Bar string

if errors.Is(err1, ErrNotSupported[Bar]{}) {
t.Fatalf("Should not be comparable")
}

err3 := ErrNotSupported[string]{}

if !errors.As(err2, &err3) {
t.Fatalf("Should be comparable")
}

if err3.Protos[0] != "/a" {
t.Fatalf("Should be read as ErrNotSupported")
}
}

0 comments on commit 9691dd5

Please sign in to comment.