1+ import logging
12from collections .abc import Awaitable , Callable
23from typing import TypeVar
34
1011from fastapi .responses import JSONResponse
1112from pydantic import ValidationError
1213
14+ from ..logging_errors import create_troubleshotting_log_kwargs
15+ from ..status_codes_utils import is_5xx_server_error
16+
1317validation_error_response_definition ["properties" ] = {
1418 "errors" : {
1519 "title" : "Validation errors" ,
2125
2226TException = TypeVar ("TException" )
2327
28+ _logger = logging .getLogger (__name__ )
29+
2430
2531def make_http_error_handler_for_exception (
2632 status_code : int ,
@@ -36,12 +42,24 @@ def make_http_error_handler_for_exception(
3642 SEE https://docs.python.org/3/library/exceptions.html#concrete-exceptions
3743 """
3844
39- async def _http_error_handler (_ : Request , exc : Exception ) -> JSONResponse :
45+ async def _http_error_handler (request : Request , exc : Exception ) -> JSONResponse :
4046 assert isinstance (exc , exception_cls ) # nosec
4147 error_content = {
4248 "errors" : error_extractor (exc ) if error_extractor else [f"{ exc } " ]
4349 }
4450
51+ if is_5xx_server_error (status_code ):
52+ _logger .exception (
53+ create_troubleshotting_log_kwargs (
54+ "Unexpected error happened in the Resource Usage Tracker. Please contact support." ,
55+ error = exc ,
56+ error_context = {
57+ "request" : request ,
58+ "request.method" : f"{ request .method } " ,
59+ },
60+ )
61+ )
62+
4563 return JSONResponse (
4664 content = jsonable_encoder (
4765 {"error" : error_content } if envelope_error else error_content
0 commit comments