Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/jsonrpc2/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (err *WireError) Error() string {

func (err *WireError) Is(other error) bool {
w, ok := other.(*WireError)
if !ok {
if !ok || w == nil {
return false
}
return err.Code == w.Code
Expand Down
24 changes: 24 additions & 0 deletions internal/jsonrpc2/wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package jsonrpc2_test
import (
"bytes"
"encoding/json"
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -147,6 +148,29 @@ func TestDecodeIDOnlyMessageIsResponse(t *testing.T) {
}
}

// TestWireErrorIs checks the errors.Is behavior of WireError, including that
// comparing against a typed-nil *WireError does not panic.
func TestWireErrorIs(t *testing.T) {
err := jsonrpc2.NewError(-32600, "invalid request")
for _, test := range []struct {
name string
target error
want bool
}{
{name: "same code", target: jsonrpc2.NewError(-32600, "other message"), want: true},
{name: "different code", target: jsonrpc2.NewError(-32601, "invalid request"), want: false},
{name: "typed nil WireError", target: (*jsonrpc2.WireError)(nil), want: false},
{name: "untyped nil", target: nil, want: false},
{name: "other error type", target: errors.New("invalid request"), want: false},
} {
t.Run(test.name, func(t *testing.T) {
if got := errors.Is(err, test.target); got != test.want {
t.Errorf("errors.Is(%v, %v) = %v, want %v", err, test.target, got, test.want)
}
})
}
}

func checkJSON(t *testing.T, got, want []byte) {
// compare the compact form, to allow for formatting differences
g := &bytes.Buffer{}
Expand Down