diff --git a/connexion/middleware/main.py b/connexion/middleware/main.py index b5ae0c0e9..fb6284ecf 100644 --- a/connexion/middleware/main.py +++ b/connexion/middleware/main.py @@ -454,7 +454,9 @@ def add_error_handler( :param code_or_exception: An exception class or the status code of HTTP exceptions to handle. - :param function: Callable that will handle exception, may be async. + :param function: Callable that will handle exception and return a HTTP problem response. + An async coroutine has access to the stack traceback. A sync function has no access + to the stack traceback because it is run in a threadpool. """ if self.middleware_stack is not None: raise RuntimeError( diff --git a/docs/exceptions.rst b/docs/exceptions.rst index dd77f68f1..3d34f53d9 100644 --- a/docs/exceptions.rst +++ b/docs/exceptions.rst @@ -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. @@ -115,7 +115,11 @@ You can register error handlers on: .. note:: - Error handlers can be ``async`` coroutines as well. + All the error handlers shown above are sync functions. The exception stack trace is + not available in a sync function because the middleware runs it in a threadpool. + + Error handlers can be ``async`` coroutines as well. Use a coroutine if the handler + function needs the stack trace from the exception, for example to log a traceback. .. _Flask documentation: https://flask.palletsprojects.com/en/latest/errorhandling/#error-handlers