Skip to content

Commit a603857

Browse files
committed
Give a more informative error for JSON not serializable.
1 parent f309cc2 commit a603857

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

dash/dash.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,22 @@ def add_context(*args, **kwargs):
521521
}
522522
}
523523

524-
return flask.Response(
525-
json.dumps(response,
526-
cls=plotly.utils.PlotlyJSONEncoder),
527-
mimetype='application/json'
528-
)
524+
try:
525+
return flask.Response(
526+
json.dumps(response,
527+
cls=plotly.utils.PlotlyJSONEncoder),
528+
mimetype='application/json'
529+
)
530+
except TypeError:
531+
raise exceptions.ReturnValueNotJSONSerializable('''
532+
Callback for property `{:s}` from component id `{:s}`
533+
returned a value `{}` of type `{:s}`,
534+
which is not JSON serializable.
535+
'''.format(
536+
output.component_property,
537+
output.component_id,
538+
output_value,
539+
type(output_value).__name__).replace(' ', ''))
529540

530541
self.callback_map[callback_id]['callback'] = add_context
531542

dash/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ class CantHaveMultipleOutputs(CallbackException):
4444

4545
class PreventUpdate(CallbackException):
4646
pass
47+
48+
49+
class ReturnValueNotJSONSerializable(CallbackException):
50+
pass

0 commit comments

Comments
 (0)