-
-
Notifications
You must be signed in to change notification settings - Fork 485
Open
Labels
EnhancementThis is a new feature or requestThis is a new feature or request
Description
Summary
this small snippet from the docs with the wrong import ie from http.client import HTTPException instead of from litestar.exceptions import HTTPException will still output exceptions in json and I found this real hard to pinpoint what was happening,
maybe we can while building the app raise a ImproperlyConfiguredException if the wrong import is detected somehow,
or add a note in the docs, the auto-import in pycharm caught me off-guard and truly wants you to import the stdlib one first.
Admittedly I should pay more attention to what I import, but well, this happens and Litestar has proven very good at making sure mistakes are not made so why not this one :)
from litestar import Litestar, MediaType, Request, Response, get
from litestar.exceptions import HTTPException
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
def plain_text_exception_handler(_: Request, exc: Exception) -> Response:
"""Default handler for exceptions subclassed from HTTPException."""
status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
detail = getattr(exc, "detail", "")
return Response(
media_type=MediaType.TEXT,
content=detail,
status_code=status_code,
)
@get("/")
async def index() -> None:
raise HTTPException(detail="an error occurred", status_code=400)
app = Litestar(
route_handlers=[index],
exception_handlers={HTTPException: plain_text_exception_handler},
)
Basic Example
No response
Drawbacks and Impact
No response
Unresolved questions
No response
Metadata
Metadata
Assignees
Labels
EnhancementThis is a new feature or requestThis is a new feature or request