Skip to content

Commit

Permalink
Consolidate common errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Oct 23, 2021
1 parent 6311b97 commit 334382b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ func (j *jmessages) parseJSON(data []byte) error {
if len(data) == 0 || data[0] != '[' {
msgs = append(msgs, nil)
if err := json.Unmarshal(data, &msgs[0]); err != nil {
return Errorf(code.ParseError, "invalid request message")
return errInvalidRequest
}
} else if err := json.Unmarshal(data, &msgs); err != nil {
return Errorf(code.ParseError, "invalid request batch")
return errInvalidRequest
} else {
batch = true
}
Expand Down
3 changes: 3 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var errClientStopped = errors.New("the client has been stopped")
// errEmptyMethod is the error reported for an empty request method name.
var errEmptyMethod = &Error{Code: code.InvalidRequest, Message: "empty method name"}

// errInvalidRequest is the error reported for an invalid request object or batch.
var errInvalidRequest = &Error{Code: code.ParseError, Message: "invalid request value"}

// errChannelClosed is the error reported to a pending callback when the client
// channel has closed before the call completed.
var errChannelClosed = &Error{Code: code.Cancelled, Message: "client channel terminated"}
Expand Down
4 changes: 2 additions & 2 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func TestParseRequests(t *testing.T) {
}, ErrInvalidVersion},

// A broken request.
{`{`, nil, Errorf(code.ParseError, "invalid request message")},
{`{`, nil, Errorf(code.ParseError, "invalid request value")},

// A broken batch.
{`["bad"{]`, nil, Errorf(code.ParseError, "invalid request batch")},
{`["bad"{]`, nil, Errorf(code.ParseError, "invalid request value")},
}
for _, test := range tests {
got, err := ParseRequests([]byte(test.input))
Expand Down
4 changes: 2 additions & 2 deletions jrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,11 @@ func TestServer_nonLibraryClient(t *testing.T) {

// A broken batch request should report a single top-level error.
{`[{"jsonrpc":"2.0", "method":"A", "id": 1}, {"jsonrpc":"2.0"]`, // N.B. syntax error
`{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"invalid request batch"}}`},
`{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"invalid request value"}}`},

// A broken single request should report a top-level error.
{`{"bogus"][++`,
`{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"invalid request message"}}`},
`{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"invalid request value"}}`},

// Various invalid ID checks.
{`{"jsonrpc":"2.0", "id":[], "method":"X"}`, invalidIDMessage}, // invalid ID: array
Expand Down

0 comments on commit 334382b

Please sign in to comment.