|
| 1 | +import logging |
| 2 | + |
| 3 | +from aiohttp import web |
| 4 | +from common_library.error_codes import create_error_code |
| 5 | +from models_library.rest_error import ErrorGet |
| 6 | +from servicelib import status_codes_utils |
1 | 7 | from servicelib.aiohttp import status |
2 | | -from simcore_service_webserver.constants import MSG_TRY_AGAIN_OR_SUPPORT |
3 | | -from simcore_service_webserver.director_v2.exceptions import DirectorV2ServiceError |
| 8 | +from servicelib.logging_errors import create_troubleshotting_log_kwargs |
4 | 9 |
|
| 10 | +from ...constants import MSG_TRY_AGAIN_OR_SUPPORT |
5 | 11 | from ...exception_handling import ( |
6 | 12 | ExceptionHandlersMap, |
7 | 13 | ExceptionToHttpErrorMap, |
8 | 14 | HttpErrorInfo, |
| 15 | + create_error_context_from_request, |
| 16 | + create_error_response, |
9 | 17 | exception_handling_decorator, |
10 | 18 | to_exceptions_handlers_map, |
11 | 19 | ) |
12 | 20 | from ...users.exceptions import UserDefaultWalletNotFoundError |
13 | 21 | from ...wallets.errors import WalletNotEnoughCreditsError |
| 22 | +from ..exceptions import DirectorV2ServiceError |
14 | 23 |
|
15 | 24 | _exceptions_handlers_map: ExceptionHandlersMap = {} |
16 | 25 |
|
17 | 26 |
|
| 27 | +_logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +async def _handler_director_service_error_as_503_or_4xx( |
| 31 | + request: web.Request, exception: Exception |
| 32 | +) -> web.Response: |
| 33 | + assert isinstance(exception, DirectorV2ServiceError) # nosec |
| 34 | + assert status_codes_utils.is_error( |
| 35 | + exception.status |
| 36 | + ), f"DirectorV2ServiceError must be an error, got {exception=}" # nosec |
| 37 | + |
| 38 | + if status_codes_utils.is_5xx_server_error(exception.status): |
| 39 | + # NOTE: All directorv2 5XX are mapped to 503 |
| 40 | + status_code = status.HTTP_503_SERVICE_UNAVAILABLE |
| 41 | + user_msg = ( |
| 42 | + # Most likely the director service is down or misconfigured so the user is asked to try again later. |
| 43 | + "This service is temporarily unavailable. The incident was logged and will be investigated. " |
| 44 | + + MSG_TRY_AGAIN_OR_SUPPORT |
| 45 | + ) |
| 46 | + |
| 47 | + # Log for further investigation |
| 48 | + oec = create_error_code(exception) |
| 49 | + _logger.exception( |
| 50 | + **create_troubleshotting_log_kwargs( |
| 51 | + user_msg, |
| 52 | + error=exception, |
| 53 | + error_code=oec, |
| 54 | + error_context={ |
| 55 | + **create_error_context_from_request(request), |
| 56 | + "error_code": oec, |
| 57 | + }, |
| 58 | + ) |
| 59 | + ) |
| 60 | + error = ErrorGet.model_construct( |
| 61 | + message=user_msg, support_id=oec, status=status_code |
| 62 | + ) |
| 63 | + |
| 64 | + else: |
| 65 | + # NOTE: All directorv2 4XX are mapped one-to-one |
| 66 | + assert status_codes_utils.is_4xx_client_error( |
| 67 | + exception.status |
| 68 | + ), f"DirectorV2ServiceError must be a client error, got {exception=}" # nosec |
| 69 | + |
| 70 | + error = ErrorGet(status=exception.status, message="{exception}") |
| 71 | + |
| 72 | + return create_error_response(error, status_code=error.status) |
| 73 | + |
| 74 | + |
| 75 | +_exceptions_handlers_map[DirectorV2ServiceError] = ( |
| 76 | + _handler_director_service_error_as_503_or_4xx |
| 77 | +) |
| 78 | + |
| 79 | + |
18 | 80 | _TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = { |
19 | 81 | UserDefaultWalletNotFoundError: HttpErrorInfo( |
20 | 82 | status.HTTP_404_NOT_FOUND, |
|
24 | 86 | status.HTTP_402_PAYMENT_REQUIRED, |
25 | 87 | "Wallet does not have enough credits for computations. {reason}", |
26 | 88 | ), |
27 | | - DirectorV2ServiceError: HttpErrorInfo( |
28 | | - status.HTTP_503_SERVICE_UNAVAILABLE, |
29 | | - # This error is raised when the director service raises an unhandled exception. |
30 | | - # Most likely the director service is down or misconfigured so the user is asked to try again later. |
31 | | - "This service is temporarily unavailable. The incident was logged and will be investigated. " |
32 | | - + MSG_TRY_AGAIN_OR_SUPPORT, |
33 | | - ), |
34 | 89 | } |
35 | 90 |
|
36 | 91 | _exceptions_handlers_map.update(to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP)) |
|
0 commit comments