Skip to content

Commit

Permalink
fix: logger internal server error
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 19, 2024
1 parent d30b6d6 commit 99d5d62
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid
from datetime import datetime
from pathlib import Path
from typing import NamedTuple
from typing import Any, NamedTuple

import asyncpg
import litestar
Expand Down Expand Up @@ -164,18 +164,20 @@ def before_req(req: litestar.Request[None, None, State]) -> None:
req.state["now"] = datetime.now(tz=UTC)


def plain_text_exception_handler(_: Request, exc: Exception) -> Template:
def plain_text_exception_handler(_: Request, exc: HTTPException) -> Template:
"""Default handler for exceptions subclassed from HTTPException."""
status_code = getattr(exc, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
detail = getattr(exc, "detail", "")

if not isinstance(exc, HTTPException):
logger.error("internal server error: {}", exc)

return Template(
"error.html.jinja2",
status_code=status_code,
context={"error": exc, "detail": detail},
status_code=exc.status_code,
context={"error": exc, "detail": exc.detail},
)


def internal_error_handler(_: Request, exc: Exception) -> Response[Any]:
logger.error("internal server error: {}", exc)
return Response(
content={"error": exc, "detail": "internal server error"},
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
)


Expand All @@ -201,6 +203,7 @@ def plain_text_exception_handler(_: Request, exc: Exception) -> Template:
middleware=[session_auth_config.middleware],
exception_handlers={
HTTPException: plain_text_exception_handler,
Exception: internal_error_handler,
},
debug=DEV,
)

0 comments on commit 99d5d62

Please sign in to comment.