Skip to content

Commit 4f80dd1

Browse files
committed
Revise exception handling doc on error handler functions
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 #2019
1 parent 1844a2f commit 4f80dd1

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

connexion/middleware/main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ def add_error_handler(
454454
455455
:param code_or_exception: An exception class or the status code of HTTP exceptions to
456456
handle.
457-
:param function: Callable that will handle exception, may be async.
457+
:param function: Callable that will handle exception and return a HTTP problem response.
458+
An async coroutine has access to the stack traceback. A sync function has no access
459+
to the stack traceback because it is run in a threadpool.
458460
"""
459461
if self.middleware_stack is not None:
460462
raise RuntimeError(

docs/exceptions.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ You can register error handlers on:
6262

6363
.. warning::
6464

65-
⚠️ **The following is not recommended as it complicates the exception handling logic,**
65+
⚠️ **The following is not recommended as it complicates the exception handling logic!**
6666

6767
You can also register error handlers on the underlying flask application directly.
6868

@@ -115,7 +115,11 @@ You can register error handlers on:
115115

116116
.. note::
117117

118-
Error handlers can be ``async`` coroutines as well.
118+
All the error handlers shown above are sync functions. The exception stack trace is
119+
not available in a sync function because the middleware runs it in a threadpool.
120+
121+
Error handlers can be ``async`` coroutines as well. Use a coroutine if the handler
122+
function needs the stack trace from the exception, for example to log a traceback.
119123

120124
.. _Flask documentation: https://flask.palletsprojects.com/en/latest/errorhandling/#error-handlers
121125

0 commit comments

Comments
 (0)