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

Add period mismatch error handling to API #1026

Merged
merged 3 commits into from
Jul 28, 2021
Merged
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

### 35.4.2 [#1026](https://github.com/openfisca/openfisca-core/pull/1026)

#### Bug fix

- [Web API] Handle a period mismatch error
- Period mismatch error was not being handled by the API
- Since it's caused by the user, a 400 (bad request error) is to be expected
- However, since it was not being handled, a 500 (internal server error) was being given instead

### 35.4.1 [#1007](https://github.com/openfisca/openfisca-core/pull/1007)

#### Bug fix
Expand Down
6 changes: 3 additions & 3 deletions openfisca_web_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import traceback

from openfisca_core.errors import SituationParsingError
from openfisca_core.errors import SituationParsingError, PeriodMismatchError
from openfisca_web_api.loader import build_data
from openfisca_web_api.errors import handle_import_error
from openfisca_web_api import handlers
Expand Down Expand Up @@ -138,7 +138,7 @@ def calculate():
input_data = request.get_json()
try:
result = handlers.calculate(tax_benefit_system, input_data)
except SituationParsingError as e:
except (SituationParsingError, PeriodMismatchError) as e:
abort(make_response(jsonify(e.error), e.code or 400))
HAEKADI marked this conversation as resolved.
Show resolved Hide resolved
except (UnicodeEncodeError, UnicodeDecodeError) as e:
abort(make_response(jsonify({"error": "'" + e[1] + "' is not a valid ASCII value."}), 400))
Expand All @@ -151,7 +151,7 @@ def trace():
input_data = request.get_json()
try:
result = handlers.trace(tax_benefit_system, input_data)
except SituationParsingError as e:
except (SituationParsingError, PeriodMismatchError) as e:
abort(make_response(jsonify(e.error), e.code or 400))
return jsonify(result)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

setup(
name = 'OpenFisca-Core',
version = '35.4.1',
version = '35.4.2',
author = 'OpenFisca Team',
author_email = 'contact@openfisca.org',
classifiers = [
Expand Down
29 changes: 29 additions & 0 deletions tests/web_api/test_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,35 @@ def test_periods(test_client):
assert monthly_variable == {'2017-01': 'tenant'}


def test_handle_period_mismatch_error(test_client):

variable = "housing_tax"
period = "2017-01"

simulation_json = json.dumps({
"persons": {
"bill": {}
},
"households": {
"_": {
"parents": ["bill"],
variable: {
period: 400
},
}
}
})

response = post_json(test_client, simulation_json)
assert response.status_code == client.BAD_REQUEST

response_json = json.loads(response.data)

error = dpath.get(response_json, f'households/_/housing_tax/{period}')
message = f'Unable to set a value for variable "{variable}" for month-long period "{period}"'
assert message in error


def test_gracefully_handle_unexpected_errors(test_client):
"""
Context
Expand Down