Send no ID for batch request errors

Fixes #54
This commit is contained in:
Neil Booth 2016-12-03 10:56:55 +09:00
parent 2edb762b83
commit 625508f5c6

View File

@ -211,16 +211,17 @@ class JSONRPC(asyncio.Protocol, LoggedClass):
if self.transport.is_closing(): if self.transport.is_closing():
return return
id_ = payload.get('id') if isinstance(payload, dict) else None
try: try:
data = (json.dumps(payload) + '\n').encode() data = (json.dumps(payload) + '\n').encode()
except TypeError: except TypeError:
msg = 'JSON encoding failure: {}'.format(payload) msg = 'JSON encoding failure: {}'.format(payload)
self.logger.error(msg) self.logger.error(msg)
self.send_json_error(msg, self.INTERNAL_ERROR, payload.get('id')) self.send_json_error(msg, self.INTERNAL_ERROR, id_)
else: else:
if len(data) > max(1000, self.max_send): if len(data) > max(1000, self.max_send):
self.send_json_error('request too large', self.INVALID_REQUEST, self.send_json_error('request too large',
payload.get('id')) self.INVALID_REQUEST, id_)
raise self.LargeRequestError raise self.LargeRequestError
else: else:
self.send_count += 1 self.send_count += 1