Skip to content

server: Add data attribute to error response #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions jsonrpclib/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ class Fault(object):
JSON-RPC error class
"""
def __init__(self, code=-32000, message='Server error', rpcid=None,
config=jsonrpclib.config.DEFAULT):
config=jsonrpclib.config.DEFAULT, data=None):
"""
Sets up the error description

Expand All @@ -795,14 +795,15 @@ def __init__(self, code=-32000, message='Server error', rpcid=None,
self.faultString = message
self.rpcid = rpcid
self.config = config
self.data = data

def error(self):
"""
Returns the error as a dictionary

:returns: A {'code', 'message'} dictionary
"""
return {'code': self.faultCode, 'message': self.faultString}
return {'code': self.faultCode, 'message': self.faultString, 'data': self.data}

def response(self, rpcid=None, version=None):
"""
Expand Down Expand Up @@ -923,7 +924,7 @@ def response(self, result=None):

return response

def error(self, code=-32000, message='Server error.'):
def error(self, code=-32000, message='Server error.', data=None):
"""
Prepares an error dictionary

Expand All @@ -937,6 +938,8 @@ def error(self, code=-32000, message='Server error.'):
else:
error['result'] = None
error['error'] = {'code': code, 'message': message}
if data is not None:
error['error']['data'] = data
return error

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -983,7 +986,7 @@ def dump(params=None, methodname=None, rpcid=None, version=None,
if isinstance(params, Fault):
# Prepare an error dictionary
# pylint: disable=E1103
return payload.error(params.faultCode, params.faultString)
return payload.error(params.faultCode, params.faultString, params.data)

if not isinstance(methodname, utils.string_types) and not is_response:
# Neither a request nor a response
Expand Down