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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion chalice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def handle_decimals(obj):
# to support that as well.
if isinstance(obj, decimal.Decimal):
return float(obj)
return obj
raise TypeError('Object of type %s is not JSON serializable'
% obj.__class__.__name__)


def error_response(message, error_code, http_status_code, headers=None):
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ def test_invalid_binary_response_body_throws_value_error(sample_app):
response.to_dict(sample_app.api.binary_types)


def test_invalid_JSON_response_body_throws_type_error(sample_app):
response = app.Response(
status_code=200,
body={'foo': object()},
headers={'Content-Type': 'application/json'}
)
with pytest.raises(TypeError):
response.to_dict()


def test_can_encode_binary_body_as_base64(sample_app):
response = app.Response(
status_code=200,
Expand Down