Skip to content

Django backend fails on exception with non-serializable argumentΒ #46

Open
@cjerdonek

Description

@cjerdonek

I encountered a situation where json-rpc will break on certain exceptions. This is with json-rpc version 1.10.3 and Django 1.9.6.

If an exception is raised with code like raise Exception(foo), where foo is not serializable, then json-rpc doesn't return a proper JSON response.

Here is an example stack trace:

Traceback (most recent call last):
  File ".../python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File ".../python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ".../python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File ".../python3.5/site-packages/jsonrpc/backend/django.py", line 65, in jsonrpc
    response = response.json
  File ".../python3.5/site-packages/jsonrpc/base.py", line 85, in json
    return self.serialize(self.data)
  File ".../python3.5/site-packages/jsonrpc/backend/django.py", line 62, in serialize
    return json.dumps(s, cls=DatetimeDecimalEncoder)
  File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File ".../python3.5/site-packages/jsonrpc/utils.py", line 53, in default
    return json.JSONEncoder.default(self, o)
  File "/usr/lib/python3.5/json/encoder.py", line 180, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Foo object> is not JSON serializable

In the case of handling an exception, it seems like json-rpc should be able to fall back to something like calling repr() on the exception, instead of insisting that the exception be serializable.

This is because the developer doesn't necessarily have control over what exceptions are raised (e.g. if they come from third-party code, etc).

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions