1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
import logging
from typing import TYPE_CHECKING
from litestar import Litestar, get
from litestar.exceptions import HTTPException
from litestar.status_codes import HTTP_400_BAD_REQUEST
logger = logging.getLogger()
if TYPE_CHECKING:
from litestar.types import Scope
@get("/some-path", sync_to_thread=False)
def my_handler() -> None:
"""Route handler that raises an exception."""
raise HTTPException(detail="bad request", status_code=HTTP_400_BAD_REQUEST)
async def after_exception_handler(exc: Exception, scope: "Scope") -> None:
"""Hook function that will be invoked after each exception."""
state = Litestar.from_scope(scope).state
if not hasattr(state, "error_count"):
state.error_count = 1
else:
state.error_count += 1
logger.info(
"an exception of type %s has occurred for requested path %s and the application error count is %d.",
type(exc).__name__,
scope["path"],
state.error_count,
)
app = Litestar([my_handler], after_exception=[after_exception_handler])
|