From 9b83ec1849a2d68dbedb2a8f07d0bfd0a44a85e5 Mon Sep 17 00:00:00 2001 From: burnettk Date: Thu, 20 Oct 2022 16:00:14 -0400 Subject: [PATCH] Squashed 'flask-bpmn/' changes from bd4b45a84..c8fd01df4 c8fd01df4 send ApiError to sentry and remove ApiError specific handler and send everything through the same one 70ee69c9b let InternalServerError be handled by the common Exception handler and do not send ApiErrors to sentry git-subtree-dir: flask-bpmn git-subtree-split: c8fd01df47518749a074772fec383256c482139f --- src/flask_bpmn/api/api_error.py | 39 +++++++++++++-------------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/flask_bpmn/api/api_error.py b/src/flask_bpmn/api/api_error.py index b106713c..8fd0b798 100644 --- a/src/flask_bpmn/api/api_error.py +++ b/src/flask_bpmn/api/api_error.py @@ -19,7 +19,6 @@ from SpiffWorkflow.bpmn.exceptions import WorkflowTaskExecException # type: ign from SpiffWorkflow.exceptions import WorkflowException # type: ignore from SpiffWorkflow.specs.base import TaskSpec # type: ignore from SpiffWorkflow.task import Task # type: ignore -from werkzeug.exceptions import InternalServerError api_error_blueprint = Blueprint("api_error_blueprint", __name__) @@ -169,24 +168,10 @@ def set_user_sentry_context() -> None: set_user({"username": username}) -@api_error_blueprint.app_errorhandler(ApiError) -def handle_invalid_usage(error: ApiError) -> flask.wrappers.Response: - """Handles invalid usage error.""" - current_app.logger.exception(error) - return make_response(jsonify(error), error.status_code) - - -@api_error_blueprint.app_errorhandler(InternalServerError) -def handle_internal_server_error(error: InternalServerError) -> flask.wrappers.Response: - """Handles internal server error.""" - original = getattr(error, "original_exception", None) - api_error = ApiError(error_code="internal_server_error", message=str(original)) - return make_response(jsonify(api_error), 500) - - @api_error_blueprint.app_errorhandler(Exception) -def handle_internal_server_exception(exception: Exception) -> flask.wrappers.Response: +def handle_exception(exception: Exception) -> flask.wrappers.Response: """Handles unexpected exceptions.""" + current_app.logger.exception(exception) set_user_sentry_context() id = capture_exception(exception) @@ -198,11 +183,17 @@ def handle_internal_server_exception(exception: Exception) -> flask.wrappers.Res f"https://sentry.io/{organization_slug}/{project_slug}/events/{id}" ) - current_app.logger.exception(exception) + # set api_exception like this to avoid confusing mypy + # and what type the object is + api_exception = None + if isinstance(exception, ApiError): + api_exception = exception + else: + api_exception = ApiError( + error_code="internal_server_error", + message=f"{exception.__class__.__name__}", + sentry_link=sentry_link, + status_code=500, + ) - api_exception = ApiError( - error_code="error", - message=f"{exception.__class__.__name__}", - sentry_link=sentry_link, - ) - return make_response(jsonify(api_exception), 500) + return make_response(jsonify(api_exception), api_exception.status_code)