From 51c01b83aec1256421db25257f1671b2fc7fa8f2 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Mon, 4 Feb 2019 12:38:18 +0100 Subject: [PATCH] rpc: respond to invalid JSON --- rpc/client.go | 3 +++ rpc/errors.go | 7 +++++++ rpc/testdata/invalid-syntax.json | 5 +++++ 3 files changed, 15 insertions(+) create mode 100644 rpc/testdata/invalid-syntax.json diff --git a/rpc/client.go b/rpc/client.go index 1b3c3ada0c..02029dc8f6 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -612,6 +612,9 @@ func (c *Client) drainRead() { func (c *Client) read(codec ServerCodec) { for { msgs, batch, err := codec.Read() + if _, ok := err.(*json.SyntaxError); ok { + codec.Write(context.Background(), errorMessage(&parseError{err.Error()})) + } if err != nil { c.readErr <- err return diff --git a/rpc/errors.go b/rpc/errors.go index 1166a5540a..c3aa826cc8 100644 --- a/rpc/errors.go +++ b/rpc/errors.go @@ -36,6 +36,13 @@ func (e *subscriptionNotFoundError) Error() string { 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 type invalidRequestError struct{ message string } diff --git a/rpc/testdata/invalid-syntax.json b/rpc/testdata/invalid-syntax.json new file mode 100644 index 0000000000..b194299603 --- /dev/null +++ b/rpc/testdata/invalid-syntax.json @@ -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"}} +