Fix FlaskApp exception handlers (#1788)

Fixes #1787 

In the `FlaskApp`, the error handlers were still registered on the
underlying Flask app instead of on the `ExceptionMiddleware`, which led
to them not following the documented behavior.

The documentation was also incorrect about ignoring error handlers
registered on the flask application. We are only ignoring the default
error handlers registered by Flask itself.

This is a breaking change, however, since this functionality was not
following the documented behavior, and 3.0.0 was only recently released,
I propose to release this as a patch version.
This commit is contained in:
Robbe Sneyders
2023-11-06 19:00:46 +01:00
committed by GitHub
parent 55bdfbaa70
commit 70084bcc4c
7 changed files with 147 additions and 64 deletions

View File

@@ -1,3 +1,4 @@
import json
from unittest import mock
import jinja2
@@ -7,6 +8,7 @@ from connexion import App
from connexion.exceptions import InvalidSpecification
from connexion.http_facts import METHODS
from connexion.json_schema import ExtendedSafeLoader
from connexion.lifecycle import ConnexionRequest, ConnexionResponse
from connexion.middleware.abstract import AbstractRoutingAPI
from connexion.options import SwaggerUIOptions
@@ -302,10 +304,15 @@ def test_add_error_handler(app_class, simple_api_spec_dir):
app = app_class(__name__, specification_dir=simple_api_spec_dir)
app.add_api("openapi.yaml")
def custom_error_handler(_request, _exception):
pass
def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse:
return ConnexionResponse(
status_code=404, body=json.dumps({"error": "NotFound"})
)
app.add_error_handler(Exception, custom_error_handler)
app.add_error_handler(500, custom_error_handler)
app.add_error_handler(404, not_found)
app.middleware._build_middleware_stack()
app_client = app.test_client()
response = app_client.get("/does_not_exist")
assert response.status_code == 404
assert response.json()["error"] == "NotFound"