Skip to content

Commit

Permalink
Merge pull request #114 from multiformats/marco/errunrecognized
Browse files Browse the repository at this point in the history
feat: New error to highlight unrecognized responses
  • Loading branch information
MarcoPolo authored Nov 14, 2024
2 parents b83ce30 + 990321d commit f700bb6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func (e ErrNotSupported[T]) Is(target error) bool {
return ok
}

type ErrUnrecognizedResponse[T StringLike] struct {
Actual T
Expected T
}

func (e ErrUnrecognizedResponse[T]) Error() string {
return fmt.Sprintf("unrecognized response. expected: %s (or na). got: %s", e.Expected, e.Actual)
}

// ErrNoProtocols is the error returned when the no protocols have been
// specified.
var ErrNoProtocols = errors.New("no protocols specified")
Expand Down Expand Up @@ -146,6 +155,9 @@ func readProto[T StringLike](proto T, r io.Reader) error {
case "na":
return ErrNotSupported[T]{[]T{proto}}
default:
return fmt.Errorf("unrecognized response: %s", tok)
return ErrUnrecognizedResponse[T]{
Actual: tok,
Expected: proto,
}
}
}

0 comments on commit f700bb6

Please sign in to comment.