Skip to content

allow certain errors to be handled by jsonrpc, without sending django error signal #40

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions jsonrpc/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
csrf_exempt = empty_dec

from django.core.serializers.json import DjangoJSONEncoder
from django.conf import settings

#allow not to log certain errors, by specyfying class names
HANDLED_ERRORS = getattr(settings, 'JSONRPC_HANDLED_ERRORS', [])

NoneType = type(None)
encode_kw = lambda p: dict([(str(k), v) for k, v in p.iteritems()])
Expand Down Expand Up @@ -172,14 +176,16 @@ def response_dict(self, request, D, is_batch=False, version_hint='1.0', json_enc
status = 200

except Error, e:
signals.got_request_exception.send(sender=self.__class__, request=request)
if not e.__class__.__name__ in HANDLED_ERRORS:
signals.got_request_exception.send(sender=self.__class__, request=request)
response['error'] = e.json_rpc_format
if version in ('1.1', '2.0') and 'result' in response:
response.pop('result')
status = e.status
except Exception, e:
# exception missed by others
signals.got_request_exception.send(sender=self.__class__, request=request)
if not e.__class__.__name__ in HANDLED_ERRORS:
signals.got_request_exception.send(sender=self.__class__, request=request)
other_error = OtherError(e)
response['error'] = other_error.json_rpc_format
status = other_error.status
Expand Down Expand Up @@ -224,13 +230,15 @@ def dispatch(self, request, method='', json_encoder=None):

json_rpc = dumps(response, cls=json_encoder)
except Error, e:
signals.got_request_exception.send(sender=self.__class__, request=request)
if not e.__class__.__name__ in HANDLED_ERRORS:
signals.got_request_exception.send(sender=self.__class__, request=request)
response['error'] = e.json_rpc_format
status = e.status
json_rpc = dumps(response, cls=json_encoder)
except Exception, e:
# exception missed by others
signals.got_request_exception.send(sender=self.__class__, request=request)
if not e.__class__.__name__ in HANDLED_ERRORS:
signals.got_request_exception.send(sender=self.__class__, request=request)
other_error = OtherError(e)
response['result'] = None
response['error'] = other_error.json_rpc_format
Expand Down