|
1 | 1 | """Defines the different exceptions that may arise in the catalog subpackage"""
|
2 | 2 |
|
| 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 |
3 | 8 | from servicelib.aiohttp import status
|
| 9 | +from servicelib.logging_errors import create_troubleshotting_log_kwargs |
| 10 | +from servicelib.rabbitmq._errors import RemoteMethodNotRegisteredError |
4 | 11 | from servicelib.rabbitmq.rpc_interfaces.catalog.errors import (
|
5 | 12 | CatalogForbiddenError,
|
6 | 13 | CatalogItemNotFoundError,
|
7 | 14 | )
|
8 | 15 |
|
9 | 16 | from ..exception_handling import (
|
| 17 | + ExceptionHandlersMap, |
10 | 18 | ExceptionToHttpErrorMap,
|
11 | 19 | HttpErrorInfo,
|
| 20 | + create_error_context_from_request, |
| 21 | + create_error_response, |
12 | 22 | exception_handling_decorator,
|
13 | 23 | to_exceptions_handlers_map,
|
14 | 24 | )
|
15 | 25 | from ..resource_usage.errors import DefaultPricingPlanNotFoundError
|
16 |
| -from .errors import DefaultPricingUnitForServiceNotFoundError |
| 26 | +from ._constants import MSG_CATALOG_SERVICE_NOT_FOUND, MSG_CATALOG_SERVICE_UNAVAILABLE |
| 27 | +from .errors import ( |
| 28 | + CatalogConnectionError, |
| 29 | + CatalogResponseError, |
| 30 | + DefaultPricingUnitForServiceNotFoundError, |
| 31 | +) |
17 | 32 |
|
18 | 33 | # mypy: disable-error-code=truthy-function
|
19 | 34 | assert CatalogForbiddenError # nosec
|
20 | 35 | assert CatalogItemNotFoundError # nosec
|
21 | 36 |
|
22 | 37 |
|
| 38 | +_logger = logging.getLogger(__name__) |
| 39 | + |
| 40 | + |
| 41 | +async def _handler_catalog_client_errors( |
| 42 | + request: web.Request, exception: Exception |
| 43 | +) -> web.Response: |
| 44 | + |
| 45 | + assert isinstance( # nosec |
| 46 | + exception, CatalogResponseError | CatalogConnectionError |
| 47 | + ), f"check mapping, got {exception=}" |
| 48 | + |
| 49 | + if ( |
| 50 | + isinstance(exception, CatalogResponseError) |
| 51 | + and exception.status == status.HTTP_404_NOT_FOUND |
| 52 | + ): |
| 53 | + error = ErrorGet( |
| 54 | + status=status.HTTP_404_NOT_FOUND, |
| 55 | + message=MSG_CATALOG_SERVICE_NOT_FOUND, |
| 56 | + ) |
| 57 | + |
| 58 | + else: |
| 59 | + # NOTE: The remaining errors are mapped to 503 |
| 60 | + status_code = status.HTTP_503_SERVICE_UNAVAILABLE |
| 61 | + user_msg = MSG_CATALOG_SERVICE_UNAVAILABLE |
| 62 | + |
| 63 | + # Log for further investigation |
| 64 | + oec = create_error_code(exception) |
| 65 | + _logger.exception( |
| 66 | + **create_troubleshotting_log_kwargs( |
| 67 | + user_msg, |
| 68 | + error=exception, |
| 69 | + error_code=oec, |
| 70 | + error_context={ |
| 71 | + **create_error_context_from_request(request), |
| 72 | + "error_code": oec, |
| 73 | + }, |
| 74 | + ) |
| 75 | + ) |
| 76 | + error = ErrorGet.model_construct( |
| 77 | + message=user_msg, |
| 78 | + support_id=oec, |
| 79 | + status=status_code, |
| 80 | + ) |
| 81 | + |
| 82 | + return create_error_response(error, status_code=error.status) |
| 83 | + |
| 84 | + |
23 | 85 | _TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = {
|
| 86 | + RemoteMethodNotRegisteredError: HttpErrorInfo( |
| 87 | + status.HTTP_503_SERVICE_UNAVAILABLE, |
| 88 | + MSG_CATALOG_SERVICE_UNAVAILABLE, |
| 89 | + ), |
| 90 | + CatalogForbiddenError: HttpErrorInfo( |
| 91 | + status.HTTP_403_FORBIDDEN, |
| 92 | + "Forbidden catalog access", |
| 93 | + ), |
24 | 94 | CatalogItemNotFoundError: HttpErrorInfo(
|
25 | 95 | status.HTTP_404_NOT_FOUND,
|
26 | 96 | "Catalog item not found",
|
|
32 | 102 | DefaultPricingUnitForServiceNotFoundError: HttpErrorInfo(
|
33 | 103 | status.HTTP_404_NOT_FOUND, "Default pricing unit not found"
|
34 | 104 | ),
|
35 |
| - CatalogForbiddenError: HttpErrorInfo( |
36 |
| - status.HTTP_403_FORBIDDEN, "Forbidden catalog access" |
37 |
| - ), |
38 | 105 | }
|
39 | 106 |
|
| 107 | + |
| 108 | +_exceptions_handlers_map: ExceptionHandlersMap = { |
| 109 | + CatalogResponseError: _handler_catalog_client_errors, |
| 110 | + CatalogConnectionError: _handler_catalog_client_errors, |
| 111 | +} |
| 112 | +_exceptions_handlers_map.update(to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP)) |
| 113 | + |
40 | 114 | handle_plugin_requests_exceptions = exception_handling_decorator(
|
41 |
| - to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP) |
| 115 | + _exceptions_handlers_map |
42 | 116 | )
|
43 | 117 |
|
44 | 118 |
|
|
0 commit comments