Skip to content

Commit bf4881f

Browse files
committed
adds exceptions in directorv2
1 parent fcf47a2 commit bf4881f

File tree

1 file changed

+17
-2
lines changed
  • services/director-v2/src/simcore_service_director_v2/api/errors

1 file changed

+17
-2
lines changed

services/director-v2/src/simcore_service_director_v2/api/errors/http_error.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
from typing import Awaitable, Callable
1+
import logging
2+
from collections.abc import Awaitable, Callable
23

34
from fastapi import HTTPException
45
from 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
58
from starlette.requests import Request
69
from starlette.responses import JSONResponse
710

11+
_logger = logging.getLogger(__name__)
12+
813

914
async 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

Comments
 (0)