From 85de9ab0bbebbc16f5eef47da97e21ef14e8f6d3 Mon Sep 17 00:00:00 2001 From: Christopher Lott Date: Tue, 17 Dec 2024 17:08:26 -0500 Subject: [PATCH] Revise exceptions doc to use async coroutine handler functions Document that a regular function has no access to the exception stack traceback Fixes #2019 --- docs/exceptions.rst | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/docs/exceptions.rst b/docs/exceptions.rst index dd77f68f1..3bef7426c 100644 --- a/docs/exceptions.rst +++ b/docs/exceptions.rst @@ -24,7 +24,7 @@ You can register error handlers on: from connexion import AsyncApp from connexion.lifecycle import ConnexionRequest, ConnexionResponse - def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse: + async def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse: return ConnexionResponse(status_code=404, body=json.dumps({"error": "NotFound"})) app = AsyncApp(__name__) @@ -45,7 +45,7 @@ You can register error handlers on: from connexion import FlaskApp from connexion.lifecycle import ConnexionRequest, ConnexionResponse - def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse: + async def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse: return ConnexionResponse(status_code=404, body=json.dumps({"error": "NotFound"})) app = FlaskApp(__name__) @@ -62,7 +62,7 @@ You can register error handlers on: .. warning:: - ⚠️ **The following is not recommended as it complicates the exception handling logic,** + ⚠️ **The following is not recommended as it complicates the exception handling logic!** You can also register error handlers on the underlying flask application directly. @@ -93,7 +93,7 @@ You can register error handlers on: from connexion import ConnexionMiddleware from connexion.lifecycle import ConnexionRequest, ConnexionResponse - def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse: + async def not_found(request: ConnexionRequest, exc: Exception) -> ConnexionResponse: return ConnexionResponse(status_code=404, body=json.dumps({"error": "NotFound"})) app = App(__name__) @@ -115,7 +115,9 @@ You can register error handlers on: .. note:: - Error handlers can be ``async`` coroutines as well. + Connexion error handlers are not required to be ``async`` coroutines. However, the + middleware must wrap a regular function to call it, and a wrapped handler function + has no access to the stack traceback from the exception. .. _Flask documentation: https://flask.palletsprojects.com/en/latest/errorhandling/#error-handlers