Skip to content

Commit

Permalink
chore: Refactor against new error implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
anth-volk committed Dec 18, 2024
1 parent 2e70641 commit 0f39c1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
14 changes: 7 additions & 7 deletions policyengine_api/routes/error_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@


@error_bp.app_errorhandler(404)
def handle_404(error) -> Response:
def response_404(error) -> Response:
"""Specific handler for 404 Not Found errors"""
return make_error_response(error, 404)


@error_bp.app_errorhandler(400)
def handle_400(error) -> Response:
def response_400(error) -> Response:
"""Specific handler for 400 Bad Request errors"""
return make_error_response(error, 400)


@error_bp.app_errorhandler(401)
def handle_401(error) -> Response:
def response_401(error) -> Response:
"""Specific handler for 401 Unauthorized errors"""
return make_error_response(error, 401)


@error_bp.app_errorhandler(403)
def handle_403(error) -> Response:
def response_403(error) -> Response:
"""Specific handler for 403 Forbidden errors"""
return make_error_response(error, 403)


@error_bp.app_errorhandler(500)
def handle_500(error) -> Response:
def response_500(error) -> Response:
"""Specific handler for 500 Internal Server errors"""
return make_error_response(error, 500)


@error_bp.app_errorhandler(HTTPException)
def handle_http_exception(error: HTTPException) -> Response:
def response_http_exception(error: HTTPException) -> Response:
"""Generic handler for HTTPException; should be raised if no specific handler is found"""
return make_error_response(str(error), error.code)


@error_bp.app_errorhandler(Exception)
def handle_generic_error(error: Exception) -> Response:
def response_generic_error(error: Exception) -> Response:
"""Handler for any unhandled exceptions"""
return make_error_response(str(error), 500)

Expand Down
21 changes: 8 additions & 13 deletions policyengine_api/routes/household_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import Blueprint, Response, request, jsonify
from werkzeug.exceptions import NotFound, InternalServerError, BadRequest
from werkzeug.exceptions import InternalServerError
import json

from policyengine_api.routes.error_routes import response_400, response_404
from policyengine_api.constants import COUNTRY_PACKAGE_VERSIONS
from policyengine_api.services.household_service import HouseholdService
from policyengine_api.utils import hash_object
Expand Down Expand Up @@ -34,7 +35,7 @@ def get_household(country_id: str, household_id: int) -> Response:
country_id, household_id
)
if household is None:
raise NotFound(f"Household #{household_id} not found.")
return response_404(f"Household #{household_id} not found.")
else:
return Response(
json.dumps(
Expand All @@ -47,8 +48,6 @@ 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 All @@ -69,7 +68,9 @@ def post_household(country_id: str) -> Response:
payload = request.json
is_payload_valid, message = validate_household_payload(payload)
if not is_payload_valid:
raise BadRequest(f"Unable to create new household; details: {message}")
return response_400(
f"Unable to create new household; details: {message}"
)

try:
# The household label appears to be unimplemented at this time,
Expand All @@ -94,8 +95,6 @@ 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 All @@ -119,7 +118,7 @@ def update_household(country_id: str, household_id: int) -> Response:
payload = request.json
is_payload_valid, message = validate_household_payload(payload)
if not is_payload_valid:
raise BadRequest(
return response_400(
f"Unable to update household #{household_id}; details: {message}"
)

Expand All @@ -133,7 +132,7 @@ def update_household(country_id: str, household_id: int) -> Response:
country_id, household_id
)
if household is None:
raise NotFound(f"Household #{household_id} not found.")
return response_404(f"Household #{household_id} not found.")

# Next, update the household
updated_household: dict = household_service.update_household(
Expand All @@ -153,10 +152,6 @@ 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
7 changes: 3 additions & 4 deletions policyengine_api/routes/simulation_analysis_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Blueprint, request, Response, stream_with_context
from werkzeug.exceptions import InternalServerError, BadRequest
from werkzeug.exceptions import InternalServerError
import json
from policyengine_api.routes.error_routes import response_400
from policyengine_api.utils.payload_validators import validate_country
from policyengine_api.services.simulation_analysis_service import (
SimulationAnalysisService,
Expand All @@ -27,7 +28,7 @@ def execute_simulation_analysis(country_id):

is_payload_valid, message = validate_sim_analysis_payload(payload)
if not is_payload_valid:
raise BadRequest(f"Invalid JSON data; details: {message}")
return response_400(f"Invalid JSON data; details: {message}")

currency: str = payload.get("currency")
selected_version: str = payload.get("selected_version")
Expand Down Expand Up @@ -68,8 +69,6 @@ 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
9 changes: 4 additions & 5 deletions policyengine_api/routes/tracer_analysis_routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from flask import Blueprint, request, Response, stream_with_context
from werkzeug.exceptions import InternalServerError, NotFound, BadRequest
from werkzeug.exceptions import InternalServerError
from policyengine_api.utils.payload_validators import (
validate_country,
validate_tracer_analysis_payload,
)
from policyengine_api.routes.error_routes import response_400, response_404
from policyengine_api.services.tracer_analysis_service import (
TracerAnalysisService,
)
Expand All @@ -21,7 +22,7 @@ def execute_tracer_analysis(country_id):

is_payload_valid, message = validate_tracer_analysis_payload(payload)
if not is_payload_valid:
raise BadRequest(f"Invalid JSON data; details: {message}")
return response_400(f"Invalid JSON data; details: {message}")

household_id = payload.get("household_id")
policy_id = payload.get("policy_id")
Expand All @@ -46,13 +47,11 @@ 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
"""
raise NotFound("No household simulation tracer found")
return response_404("No household simulation tracer found")
except Exception as e:
raise InternalServerError(
f"An error occurred while executing the tracer analysis. Details: {str(e)}"
Expand Down

0 comments on commit 0f39c1b

Please sign in to comment.