Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(FAQ): add entry for error tracebacks in ASGI apps #2395

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/user/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,48 @@ Alternatively, you can set the Cookie header directly as demonstrated in this ve
To include multiple values, simply use ``"; "`` to separate each name-value
pair. For example, if you were to pass ``{'Cookie': 'xxx=yyy; hello=world'}``,
you would get ``{'cookies': {'xxx': 'yyy', 'hello': 'world'}}``.

Why do I not see error tracebacks in ASGI applications?
-------------------------------------------------------

When using Falcon with ASGI servers like Uvicorn,
you might notice that server errors do not display a traceback by default.
This behavior differs from WSGI applications, where errors are logged to `stderr`,
providing detailed tracebacks.

The reason for this is that ASGI does not define a standardized way to log errors back to the application server,
unlike WSGI. Therefore, you need to configure logging manually to see these tracebacks.

Here’s how to set up logging in your ASGI Falcon application to capture error tracebacks:

.. code:: python

import logging
import falcon
import falcon.asgi

logging.basicConfig(
format="%(asctime)s [%(levelname)s] %(message)s",
level=logging.INFO
)

class ThingsResource:
async def on_get(self, req, resp):
raise ValueError('foo')

app = falcon.asgi.App()
things = ThingsResource()
app.add_route('/things', things)

By adding the above logging configuration, you will see tracebacks like this in your console:

.. code-block:: none

[ERROR] [FALCON] Unhandled exception in ASGI app
vytas7 marked this conversation as resolved.
Show resolved Hide resolved
Traceback (most recent call last):
File "<...>", line 12, in on_get
raise ValueError('foo')
ValueError: foo

For additional details on this topic,
please refer to :ref:`debugging-asgi-applications`.
6 changes: 2 additions & 4 deletions docs/user/tutorial-asgi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,8 @@ adding ``--cov-fail-under=100`` (or any other percent threshold) to our
tests in multiple environments would most probably involve running
``coverage`` directly, and combining results.

.. _debugging-asgi-applications:

Debugging ASGI Applications
---------------------------
(This section also applies to WSGI applications)
Expand All @@ -979,10 +981,8 @@ your ASGI Falcon application:
.. code:: python

import logging

import falcon


logging.basicConfig(level=logging.INFO)

class ErrorResource:
Expand All @@ -992,7 +992,6 @@ your ASGI Falcon application:
app = falcon.App()
app.add_route('/error', ErrorResource())


When the above route is accessed, Falcon will catch the unhandled exception and
automatically log an error message. Below is an example of what the log output
might look like:
Expand All @@ -1007,7 +1006,6 @@ might look like:
raise Exception("Something went wrong!")
Exception: Something went wrong!


.. note::
While logging is helpful for development and debugging, be mindful of logging
sensitive information. Ensure that log files are stored securely and are not
Expand Down