reactpy
reactpy copied to clipboard
Better Traceback for JSON Serialization Failures
Discussed in https://github.com/idom-team/idom/discussions/927
Originally posted by numpde February 13, 2023
The following grim traceback was caused by writing button(disabled=mutation) instead of ...=mutation.loading, and it's representative of a fairly common set. It would be great to have a pointer to near the origin of the error.
Exception inside application: Object of type Mutation is not JSON serializable
...
Traceback (most recent call last):
File "REPO/venv-3.11/lib/python3.11/site-packages/django_idom/websocket/consumer.py", line 48, in disconnect
await self._idom_dispatcher_future
File "REPO/venv-3.11/lib/python3.11/site-packages/django_idom/websocket/consumer.py", line 124, in _run_dispatch_loop
await serve_layout(
File "REPO/venv-3.11/lib/python3.11/site-packages/idom/core/serve.py", line 41, in serve_layout
async with create_task_group() as task_group:
File "REPO/venv-3.11/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 662, in __aexit__
raise exceptions[0]
File "REPO/venv-3.11/lib/python3.11/site-packages/idom/core/serve.py", line 52, in _single_outgoing_loop
await send(await layout.render())
File "REPO/venv-3.11/lib/python3.11/site-packages/channels/generic/websocket.py", line 271, in send_json
await super().send(text_data=await self.encode_json(content), close=close)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "REPO/venv-3.11/lib/python3.11/site-packages/channels/generic/websocket.py", line 279, in encode_json
return json.dumps(content)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/json/encoder.py", line 200, in encode
chunks = self.iterencode(o, _one_shot=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/json/encoder.py", line 258, in iterencode
return _iterencode(o, 0)
^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/json/encoder.py", line 180, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Mutation is not JSON serializable
As per this comment we should report a more proximal error when in debug mode (for the sake of performance)? If we do this, then it probably makes sense to also catch serialization errors and suggest running in debug mode to get a more useful traceback.