rpc: respond to invalid JSON

This commit is contained in:
Felix Lange 2019-02-04 12:38:18 +01:00
parent a0ecea3fa7
commit 51c01b83ae
3 changed files with 15 additions and 0 deletions

View file

@ -612,6 +612,9 @@ func (c *Client) drainRead() {
func (c *Client) read(codec ServerCodec) { func (c *Client) read(codec ServerCodec) {
for { for {
msgs, batch, err := codec.Read() msgs, batch, err := codec.Read()
if _, ok := err.(*json.SyntaxError); ok {
codec.Write(context.Background(), errorMessage(&parseError{err.Error()}))
}
if err != nil { if err != nil {
c.readErr <- err c.readErr <- err
return return

View file

@ -36,6 +36,13 @@ func (e *subscriptionNotFoundError) Error() string {
return fmt.Sprintf("no %q subscription in %s namespace", e.subscription, e.namespace) return fmt.Sprintf("no %q subscription in %s namespace", e.subscription, e.namespace)
} }
// Invalid JSON was received by the server.
type parseError struct{ message string }
func (e *parseError) ErrorCode() int { return -32700 }
func (e *parseError) Error() string { return e.message }
// received message isn't a valid request // received message isn't a valid request
type invalidRequestError struct{ message string } type invalidRequestError struct{ message string }

5
rpc/testdata/invalid-syntax.json vendored Normal file
View file

@ -0,0 +1,5 @@
// This test checks that an error is written for invalid JSON requests.
--> 'f
<-- {"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"invalid character '\\'' looking for beginning of value"}}