From 0f39c1bf5449a0696dd4b731a8ce681ead51a1b7 Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Wed, 18 Dec 2024 02:34:11 +0100 Subject: [PATCH] chore: Refactor against new error implementation --- policyengine_api/routes/error_routes.py | 14 ++++++------- policyengine_api/routes/household_routes.py | 21 +++++++------------ .../routes/simulation_analysis_routes.py | 7 +++---- .../routes/tracer_analysis_routes.py | 9 ++++---- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/policyengine_api/routes/error_routes.py b/policyengine_api/routes/error_routes.py index c257875f..e9fced1c 100644 --- a/policyengine_api/routes/error_routes.py +++ b/policyengine_api/routes/error_routes.py @@ -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) diff --git a/policyengine_api/routes/household_routes.py b/policyengine_api/routes/household_routes.py index ab54f675..a160cc6e 100644 --- a/policyengine_api/routes/household_routes.py +++ b/policyengine_api/routes/household_routes.py @@ -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 @@ -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( @@ -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)}" @@ -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, @@ -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)}" @@ -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}" ) @@ -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( @@ -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)}" diff --git a/policyengine_api/routes/simulation_analysis_routes.py b/policyengine_api/routes/simulation_analysis_routes.py index b3299d72..fcbc2018 100644 --- a/policyengine_api/routes/simulation_analysis_routes.py +++ b/policyengine_api/routes/simulation_analysis_routes.py @@ -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, @@ -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") @@ -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)}" diff --git a/policyengine_api/routes/tracer_analysis_routes.py b/policyengine_api/routes/tracer_analysis_routes.py index 95524aa3..0d86179c 100644 --- a/policyengine_api/routes/tracer_analysis_routes.py +++ b/policyengine_api/routes/tracer_analysis_routes.py @@ -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, ) @@ -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") @@ -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)}"