Skip to content

Commit 539bf5d

Browse files
committed
rename
1 parent 8ec7a3d commit 539bf5d

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

services/api-server/src/simcore_service_api_server/errors/exception_handlers.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
from ..models.custom_errors import CustomBaseError
1010
from ..services.log_streaming import LogDistributionBaseException
1111
from .custom_errors import custom_error_handler
12-
from .http_error import http_error_handler, make_http_error_handler_for_exception
12+
from .http_error import http_error_handler, make_handler_for_exception
1313
from .httpx_client_error import handle_httpx_client_exceptions
1414
from .log_handling_error import log_handling_error_handler
1515
from .validation_error import http422_error_handler
1616

17+
MSG_INTERNAL_ERROR_USER_FRIENDLY_TEMPLATE = "Oops! Something went wrong, but we've noted it down and we'll sort it out ASAP. Thanks for your patience!"
18+
1719

1820
def setup(app: FastAPI):
1921
settings: ApplicationSettings = app.state.settings
@@ -28,18 +30,18 @@ def setup(app: FastAPI):
2830
# SEE https://docs.python.org/3/library/exceptions.html#exception-hierarchy
2931
app.add_exception_handler(
3032
NotImplementedError,
31-
make_http_error_handler_for_exception(
33+
make_handler_for_exception(
3234
NotImplementedError,
3335
status.HTTP_501_NOT_IMPLEMENTED,
34-
detail_message="Endpoint not implemented",
36+
error_message="This endpoint is still not implemented (under development)",
3537
),
3638
)
3739
app.add_exception_handler(
3840
Exception,
39-
make_http_error_handler_for_exception(
41+
make_handler_for_exception(
4042
Exception,
4143
status.HTTP_500_INTERNAL_SERVER_ERROR,
42-
detail_message="Unexpected error",
44+
error_message=MSG_INTERNAL_ERROR_USER_FRIENDLY_TEMPLATE,
4345
add_exception_to_message=(settings.SC_BOOT_MODE == BootModeEnum.DEBUG),
4446
add_oec_to_message=True,
4547
),

services/api-server/src/simcore_service_api_server/errors/http_error.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections.abc import Callable
23

34
from fastapi import HTTPException
@@ -8,6 +9,8 @@
89

910
from ..models.schemas.errors import ErrorGet
1011

12+
_logger = logging.getLogger(__file__)
13+
1114

1215
def create_error_json_response(*errors, status_code: int) -> JSONResponse:
1316
# NOTE: do not forget to add in the decorator `responses={ ???: {"model": ErrorGet} }`
@@ -20,11 +23,11 @@ async def http_error_handler(_: Request, exc: HTTPException) -> JSONResponse:
2023
return create_error_json_response(exc.detail, status_code=exc.status_code)
2124

2225

23-
def make_http_error_handler_for_exception(
26+
def make_handler_for_exception(
2427
exception_cls: type[BaseException],
2528
status_code: int,
2629
*,
27-
detail_message: str,
30+
error_message: str,
2831
add_exception_to_message: bool = False,
2932
add_oec_to_message: bool = False,
3033
) -> Callable:
@@ -35,13 +38,22 @@ def make_http_error_handler_for_exception(
3538
SEE https://docs.python.org/3/library/exceptions.html#concrete-exceptions
3639
"""
3740

38-
async def _http_error_handler(_: Request, error: BaseException) -> JSONResponse:
39-
assert isinstance(error, exception_cls) # nosec
40-
details = detail_message
41+
async def _http_error_handler(_: Request, exception: BaseException) -> JSONResponse:
42+
assert isinstance(exception, exception_cls) # nosec
43+
44+
msg = error_message
4145
if add_exception_to_message:
42-
details += f"\n{error}"
46+
msg += f" {exception}"
47+
4348
if add_oec_to_message:
44-
details += f"\n[OEC: {create_error_code(error)}]"
45-
return create_error_json_response(details, status_code=status_code)
49+
error_code = create_error_code(exception)
50+
msg += f" [{error_code}]"
51+
_logger.exception(
52+
"Unexpected %s: %s",
53+
exception.__class__.__name__,
54+
msg,
55+
extra={"error_code": error_code},
56+
)
57+
return create_error_json_response(msg, status_code=status_code)
4658

4759
return _http_error_handler

0 commit comments

Comments
 (0)