Skip to content

Commit de6ff9b

Browse files
committed
add logging of 500
1 parent 3907cf9 commit de6ff9b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

packages/service-library/src/servicelib/fastapi/http_error.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections.abc import Awaitable, Callable
23
from typing import TypeVar
34

@@ -10,6 +11,9 @@
1011
from fastapi.responses import JSONResponse
1112
from pydantic import ValidationError
1213

14+
from ..logging_errors import create_troubleshotting_log_kwargs
15+
from ..status_codes_utils import is_5xx_server_error
16+
1317
validation_error_response_definition["properties"] = {
1418
"errors": {
1519
"title": "Validation errors",
@@ -21,6 +25,8 @@
2125

2226
TException = TypeVar("TException")
2327

28+
_logger = logging.getLogger(__name__)
29+
2430

2531
def 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

Comments
 (0)