Skip to content

Commit

Permalink
fix: Fix manual raising within try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
anth-volk committed Dec 13, 2024
1 parent c668a92 commit a40e430
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
11 changes: 10 additions & 1 deletion policyengine_api/routes/household_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def get_household(country_id: str, household_id: int) -> Response:
household: dict | None = household_service.get_household(
country_id, household_id
)
print("Household in get_household:")
print(household)
if household is None:
raise NotFound(f"Household #{household_id} not found.")
else:
Expand All @@ -47,6 +49,8 @@ def get_household(country_id: str, household_id: int) -> Response:
status=200,
mimetype="application/json",
)
except NotFound:
raise
except Exception as e:
raise InternalServerError(
f"An error occurred while fetching household #{household_id}. Details: {str(e)}"
Expand Down Expand Up @@ -92,7 +96,8 @@ def post_household(country_id: str) -> Response:
status=201,
mimetype="application/json",
)

except BadRequest:
raise
except Exception as e:
raise InternalServerError(
f"An error occurred while creating a new household. Details: {str(e)}"
Expand Down Expand Up @@ -150,6 +155,10 @@ def update_household(country_id: str, household_id: int) -> Response:
status=200,
mimetype="application/json",
)
except BadRequest:
raise
except NotFound:
raise
except Exception as e:
raise InternalServerError(
f"An error occurred while updating household #{household_id}. Details: {str(e)}"
Expand Down
2 changes: 2 additions & 0 deletions policyengine_api/routes/simulation_analysis_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def execute_simulation_analysis(country_id):
response.headers["X-Accel-Buffering"] = "no"

return response
except BadRequest:
raise
except Exception as e:
raise InternalServerError(
f"An error occurred while executing the simulation analysis. Details: {str(e)}"
Expand Down
2 changes: 2 additions & 0 deletions policyengine_api/routes/tracer_analysis_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def execute_tracer_analysis(country_id):
response.headers["X-Accel-Buffering"] = "no"

return response
except BadRequest:
raise
except KeyError as e:
"""
This exception is raised when the tracer can't find a household tracer record
Expand Down
4 changes: 4 additions & 0 deletions policyengine_api/services/household_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ def get_household(self, country_id: str, household_id: int) -> dict | None:
f"SELECT * FROM household WHERE id = ? AND country_id = ?",
(household_id, country_id),
).fetchone()
print("Row:")
print(row)
print("Dict of row:")

# If row is present, we must JSON.loads the household_json
household = None
if row is not None:
print("Row is not None")
household = dict(row)
if household["household_json"]:
household["household_json"] = json.loads(
Expand Down
12 changes: 10 additions & 2 deletions tests/python/test_household.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def test_get_nonexistent_household(self, rest_client, mock_database):
mock_database.query().fetchone.return_value = None

response = rest_client.get("/us/household/999")
print("Response:")
print(response)
data = json.loads(response.data)
print("Data:")
print(data)

assert response.status_code == 404
assert data["status"] == "error"
Expand All @@ -49,7 +53,9 @@ def test_get_household_invalid_id(self, rest_client):
response = rest_client.get("/us/household/invalid")

assert response.status_code == 404
assert b"Invalid household ID" in response.data
assert (
b"The requested URL was not found on the server" in response.data
)


class TestCreateHousehold:
Expand Down Expand Up @@ -255,7 +261,9 @@ def test_get_household_invalid_id(self, rest_client, invalid_id):

# Default Werkzeug validation returns 404, not 400
assert response.status_code == 404
assert b"Invalid household ID" in response.data
assert (
b"The requested URL was not found on the server" in response.data
)

@pytest.mark.parametrize(
"country_id",
Expand Down

0 comments on commit a40e430

Please sign in to comment.