Bug: exception_handlers are not applied when debug flag is False #3581
-
DescriptionI want to configure a custom exception handler for the following use case. I want to run the app in production but log stack traces of internal server errors but other HTTPException should not be logged with stack trace as they are usually triggered specifically and converted to a proper response by Litestar. URL to code causing the issueNo response MCVEfrom litestar.exceptions import HTTPException
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
from litestar import Request, Response
from litestar import Litestar, post
import logging
from litestar.exceptions import LitestarException
def exception_handler(_: Request, exc: Exception) -> Response:
if isinstance(exc, HTTPException):
status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
detail = getattr(exc, "detail", "")
return Response(
content={"detail": detail},
status_code=status_code,
)
logging.exception(exc)
status_code = HTTP_500_INTERNAL_SERVER_ERROR
detail = {"special_detail": "Internal Server Error"}
return Response(content=detail, status_code=status_code)
@post(
"/test",
)
async def test() -> Response[str]:
1 / 0
return Response("test")
app = Litestar(
route_handlers=[test],
exception_handlers={LitestarException: exception_handler},
) Steps to reproduce1. Run the code
2. Trigger the endpoint
3. response body shows detail property instead of special_detail Screenshots"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)" LogsINFO: Started server process [48233]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:38254 - "GET /schema/swagger HTTP/1.1" 200 OK
INFO: 127.0.0.1:38254 - "POST /test HTTP/1.1" 500 Internal Server Error Litestar Version2.9.0 Platform
Note While we are open for sponsoring on GitHub Sponsors and Check out all issues funded or available for funding on our Polar.sh dashboard
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Not sure I understood this, you are applying the exception handler to Your MCVE never applies the handler even if debug = True for the same reason stated above |
Beta Was this translation helpful? Give feedback.
-
Thanks, I see my mistake. I assumed the LitestarException map would catch all exceptions for some reason. I checked the code and if I use the correct error mapping it works. Here is the working example for completeness: from litestar.exceptions import HTTPException
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
from litestar import Request, Response, Litestar, post
import logging
def exception_handler(_: Request, exc: Exception) -> Response:
if isinstance(exc, HTTPException) and exc.status_code != HTTP_500_INTERNAL_SERVER_ERROR:
status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
detail = getattr(exc, "detail", "")
return Response(
content={"detail": detail},
status_code=status_code,
)
logging.exception(exc)
status_code = HTTP_500_INTERNAL_SERVER_ERROR
detail = {"special_detail": "Internal Server Error"}
return Response(content=detail, status_code=status_code)
@post(
"/test",
)
async def test() -> Response[str]:
1 / 0
return Response("test")
app = Litestar(
route_handlers=[test],
exception_handlers={Exception: exception_handler},
) Now I can run with stacktraces in production without debug mode leaking the traces |
Beta Was this translation helpful? Give feedback.
Thanks, I see my mistake. I assumed the LitestarException map would catch all exceptions for some reason. I checked the code and if I use the correct error mapping it works. Here is the working example for completeness: