Fixed handle_decimals to raise a TypeError if it can't encode the object #1100
+12
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue #, if available:
Description of changes:
default Json Encoders should raise a TypeError if they can't properly encode an object.
At the moment when you try to encode an object that is not JSON serializable you get:
[ERROR] ValueError: Circular reference detected
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 820, in call
response = response.to_dict(self.api.binary_types)
File "/var/task/chalice/app.py", line 352, in to_dict
default=handle_decimals)
File "/var/lang/lib/python3.7/json/init.py", line 238, in dumps
**kw).encode(obj)
File "/var/lang/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/var/lang/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
Now you get:
[ERROR] TypeError: Object of type dict_values is not JSON serializable
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 820, in call
response = response.to_dict(self.api.binary_types)
File "/var/task/chalice/app.py", line 352, in to_dict
default=handle_decimals)
File "/var/lang/lib/python3.7/json/init.py", line 238, in dumps
**kw).encode(obj)
File "/var/lang/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/var/lang/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/var/task/chalice/app.py", line 55, in handle_decimals
raise TypeError('Object of type %s is not JSON serializable' % obj.class.name)
which conforms to what Python expects:
https://docs.python.org/2/library/json.html#json.JSONEncoder
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.