Skip to content
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

Fixed handle_decimals to raise a TypeError if it can't encode the object #1100

Closed
wants to merge 3 commits into from
Closed

Fixed handle_decimals to raise a TypeError if it can't encode the object #1100

wants to merge 3 commits into from

Conversation

subssn21
Copy link
Contributor

@subssn21 subssn21 commented Apr 5, 2019

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.

@codecov-io
Copy link

codecov-io commented Apr 5, 2019

Codecov Report

Merging #1100 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1100      +/-   ##
==========================================
+ Coverage   95.47%   95.52%   +0.04%     
==========================================
  Files          27       27              
  Lines        4577     4577              
  Branches      579      579              
==========================================
+ Hits         4370     4372       +2     
+ Misses        135      134       -1     
+ Partials       72       71       -1
Impacted Files Coverage Δ
chalice/app.py 97.31% <100%> (+0.31%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 10d7fb5...afbe522. Read the comment docs.

@stealthycoin
Copy link
Contributor

Thank you for the contribution, I pushed a changelog commit, and changed the name of a function to make more sense in the context of the stack trace it generates.

New PR: #1117.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants