From 02a53cc4c3d6073e8f961b64551831efd2833def Mon Sep 17 00:00:00 2001 From: KonstantAnxiety <58992437+KonstantAnxiety@users.noreply.github.com> Date: Wed, 20 Dec 2023 12:45:09 +0300 Subject: [PATCH] BI-5139 Handle US read-only mode (#184) --- .../dl_api_lib/app/control_api/resources/__init__.py | 11 ++++++++++- lib/dl_api_lib/dl_api_lib/error_handling.py | 1 + lib/dl_core/dl_core/exc.py | 8 ++++++++ lib/dl_core/dl_core/united_storage_client.py | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/dl_api_lib/dl_api_lib/app/control_api/resources/__init__.py b/lib/dl_api_lib/dl_api_lib/app/control_api/resources/__init__.py index b8a610389..dd60f91b9 100644 --- a/lib/dl_api_lib/dl_api_lib/app/control_api/resources/__init__.py +++ b/lib/dl_api_lib/dl_api_lib/app/control_api/resources/__init__.py @@ -6,7 +6,10 @@ import flask from flask import request from flask_restx import Api -from werkzeug.exceptions import BadRequest +from werkzeug.exceptions import ( + BadRequest, + UnavailableForLegalReasons, +) from dl_api_commons.access_control_common import AuthFailureError from dl_api_commons.flask.middlewares.commit_rci_middleware import ReqCtxInfoMiddleware @@ -41,6 +44,12 @@ def handle_us_error(error): # type: ignore return {"message": text}, resp.status_code +@API.errorhandler(UnavailableForLegalReasons) +def handle_us_read_only_mode_error(error): # type: ignore + # flask_restx doesn't support HTTP 451, so we have to handle it manually + return error.data, 451 + + @API.errorhandler(BadRequest) def handle_bad_request(error): # type: ignore rci = ReqCtxInfoMiddleware.get_last_resort_rci() diff --git a/lib/dl_api_lib/dl_api_lib/error_handling.py b/lib/dl_api_lib/dl_api_lib/error_handling.py index 592443c78..76b28ba66 100644 --- a/lib/dl_api_lib/dl_api_lib/error_handling.py +++ b/lib/dl_api_lib/dl_api_lib/error_handling.py @@ -45,6 +45,7 @@ common_exc.USPermissionCheckError: 530, common_exc.USLockUnacquiredException: status.LOCKED, common_exc.USBadRequestException: status.BAD_REQUEST, + common_exc.USReadOnlyModeEnabledException: status.UNAVAILABLE_FOR_LEGAL_REASONS, common_exc.USIncorrectTenantIdException: status.CONFLICT, common_exc.QueryConstructorError: status.BAD_REQUEST, common_exc.ResultRowCountLimitExceeded: status.BAD_REQUEST, diff --git a/lib/dl_core/dl_core/exc.py b/lib/dl_core/dl_core/exc.py index b545faada..69c5b9f46 100644 --- a/lib/dl_core/dl_core/exc.py +++ b/lib/dl_core/dl_core/exc.py @@ -260,6 +260,14 @@ def message(self) -> str: return "US permission check error" +class USReadOnlyModeEnabledException(USReqException): + err_code = USReqException.err_code + ["READ_ONLY"] + + @property + def message(self) -> str: + return "The service is currently in read-only mode" + + class USBadRequestException(USReqException): err_code = USReqException.err_code + ["BAD_REQUEST"] diff --git a/lib/dl_core/dl_core/united_storage_client.py b/lib/dl_core/dl_core/united_storage_client.py index c9d79594d..fe1772e60 100644 --- a/lib/dl_core/dl_core/united_storage_client.py +++ b/lib/dl_core/dl_core/united_storage_client.py @@ -245,6 +245,7 @@ def json(self) -> dict: (409, re.compile("The entry already exists"), exc.USAlreadyExistsException), (409, None, exc.USIncorrectTenantIdException), (423, None, exc.USLockUnacquiredException), + (451, None, exc.USReadOnlyModeEnabledException), (530, None, exc.USPermissionCheckError), ]