1- from typing import Awaitable , Callable
1+ import logging
2+ from collections .abc import Awaitable , Callable
23
34from fastapi import HTTPException
45from fastapi .encoders import jsonable_encoder
6+ from servicelib .logging_errors import create_troubleshootting_log_kwargs
7+ from servicelib .status_codes_utils import is_5xx_server_error
58from starlette .requests import Request
69from starlette .responses import JSONResponse
710
11+ _logger = logging .getLogger (__name__ )
12+
813
914async def http_error_handler (_ : Request , exc : Exception ) -> JSONResponse :
1015 assert isinstance (exc , HTTPException )
@@ -24,8 +29,18 @@ def make_http_error_handler_for_exception(
2429 SEE https://docs.python.org/3/library/exceptions.html#concrete-exceptions
2530 """
2631
27- async def _http_error_handler (_ : Request , exc : Exception ) -> JSONResponse :
32+ async def _http_error_handler (request : Request , exc : Exception ) -> JSONResponse :
2833 assert isinstance (exc , exception_cls ) # nosec
34+
35+ if is_5xx_server_error (status_code ):
36+ _logger .exception (
37+ ** create_troubleshootting_log_kwargs (
38+ f"HTTP error handler caught an { exception_cls .__name__ } exception and responds with { status_code } status code" ,
39+ error = exc ,
40+ error_context = {"request" : request , "status_code" : status_code },
41+ )
42+ )
43+
2944 return JSONResponse (
3045 content = jsonable_encoder ({"errors" : [str (exc )]}), status_code = status_code
3146 )
0 commit comments