Skip to content

Commit

Permalink
Revise exception handling doc on error handler functions
Browse files Browse the repository at this point in the history
Document that a sync function is run in a threadpool and has no access
to the exception stack traceback, but an async function does.
Extend the `add_error_handler` function's Pydoc similarly

Fixes spec-first#2019
  • Loading branch information
chrisinmtown committed Dec 23, 2024
1 parent 1844a2f commit 4f80dd1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion connexion/middleware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 6 additions & 2 deletions docs/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 4f80dd1

Please sign in to comment.