mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
rpc: respond to invalid JSON
This commit is contained in:
parent
a0ecea3fa7
commit
51c01b83ae
3 changed files with 15 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
||||
|
|
|
|||
5
rpc/testdata/invalid-syntax.json
vendored
Normal file
5
rpc/testdata/invalid-syntax.json
vendored
Normal 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"}}
|
||||
|
||||
Loading…
Reference in a new issue