Skip to content

Commit 6735374

Browse files
committed
🐛 Enhance error handling in catalog service with detailed logging and response mapping
1 parent 023b59f commit 6735374

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

services/web/server/src/simcore_service_webserver/catalog/_controller_rest_exceptions.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,84 @@
11
"""Defines the different exceptions that may arise in the catalog subpackage"""
22

3+
import logging
4+
5+
from aiohttp import web
6+
from common_library.error_codes import create_error_code
7+
from models_library.rest_error import ErrorGet
38
from servicelib.aiohttp import status
9+
from servicelib.logging_errors import create_troubleshotting_log_kwargs
410
from servicelib.rabbitmq.rpc_interfaces.catalog.errors import (
511
CatalogForbiddenError,
612
CatalogItemNotFoundError,
713
)
814

915
from ..exception_handling import (
16+
ExceptionHandlersMap,
1017
ExceptionToHttpErrorMap,
1118
HttpErrorInfo,
19+
create_error_context_from_request,
20+
create_error_response,
1221
exception_handling_decorator,
1322
to_exceptions_handlers_map,
1423
)
1524
from ..resource_usage.errors import DefaultPricingPlanNotFoundError
16-
from .errors import DefaultPricingUnitForServiceNotFoundError
25+
from ._constants import MSG_CATALOG_SERVICE_NOT_FOUND, MSG_CATALOG_SERVICE_UNAVAILABLE
26+
from .errors import (
27+
CatalogConnectionError,
28+
CatalogResponseError,
29+
DefaultPricingUnitForServiceNotFoundError,
30+
)
1731

1832
# mypy: disable-error-code=truthy-function
1933
assert CatalogForbiddenError # nosec
2034
assert CatalogItemNotFoundError # nosec
2135

2236

37+
_logger = logging.getLogger(__name__)
38+
39+
40+
async def _handler_catalog_response_errors(
41+
request: web.Request, exception: Exception
42+
) -> web.Response:
43+
44+
assert isinstance( # nosec
45+
exception, CatalogResponseError | CatalogConnectionError
46+
), f"check mapping, got {exception=}"
47+
48+
if (
49+
isinstance(exception, CatalogResponseError)
50+
and exception.status == status.HTTP_404_NOT_FOUND
51+
):
52+
error = ErrorGet(
53+
status=status.HTTP_404_NOT_FOUND,
54+
message=MSG_CATALOG_SERVICE_NOT_FOUND,
55+
)
56+
57+
else:
58+
# NOTE: The remaining errors are mapped to 503
59+
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
60+
user_msg = MSG_CATALOG_SERVICE_UNAVAILABLE
61+
62+
# Log for further investigation
63+
oec = create_error_code(exception)
64+
_logger.exception(
65+
**create_troubleshotting_log_kwargs(
66+
user_msg,
67+
error=exception,
68+
error_code=oec,
69+
error_context={
70+
**create_error_context_from_request(request),
71+
"error_code": oec,
72+
},
73+
)
74+
)
75+
error = ErrorGet.model_construct(
76+
message=user_msg, support_id=oec, status=status_code
77+
)
78+
79+
return create_error_response(error, status_code=error.status)
80+
81+
2382
_TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = {
2483
CatalogItemNotFoundError: HttpErrorInfo(
2584
status.HTTP_404_NOT_FOUND,
@@ -37,8 +96,15 @@
3796
),
3897
}
3998

99+
100+
_exceptions_handlers_map: ExceptionHandlersMap = {
101+
CatalogResponseError: _handler_catalog_response_errors,
102+
CatalogConnectionError: _handler_catalog_response_errors,
103+
}
104+
_exceptions_handlers_map.update(to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP))
105+
40106
handle_plugin_requests_exceptions = exception_handling_decorator(
41-
to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP)
107+
_exceptions_handlers_map
42108
)
43109

44110

0 commit comments

Comments
 (0)