Skip to content

Commit 5cace5b

Browse files
committed
@pcrespov use new exception handling system
1 parent 2678c71 commit 5cace5b

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from . import _service, _service_catalog_rest_client
4242
from ._exceptions import (
4343
DefaultPricingUnitForServiceNotFoundError,
44-
reraise_catalog_exceptions_as_http_errors,
44+
handle_plugin_requests_exceptions,
4545
)
4646
from .controller_rest_schemas import (
4747
CatalogRequestContext,
@@ -63,7 +63,7 @@
6363
)
6464
@login_required
6565
@permission_required("services.catalog.*")
66-
@reraise_catalog_exceptions_as_http_errors
66+
@handle_plugin_requests_exceptions
6767
async def list_services_latest(request: Request):
6868
request_ctx = CatalogRequestContext.create(request)
6969
query_params: ListServiceParams = parse_request_query_parameters_as(
@@ -101,7 +101,7 @@ async def list_services_latest(request: Request):
101101
)
102102
@login_required
103103
@permission_required("services.catalog.*")
104-
@reraise_catalog_exceptions_as_http_errors
104+
@handle_plugin_requests_exceptions
105105
async def get_service(request: Request):
106106
request_ctx = CatalogRequestContext.create(request)
107107
path_params = parse_request_path_parameters_as(ServicePathParams, request)
@@ -127,7 +127,7 @@ async def get_service(request: Request):
127127
)
128128
@login_required
129129
@permission_required("services.catalog.*")
130-
@reraise_catalog_exceptions_as_http_errors
130+
@handle_plugin_requests_exceptions
131131
async def update_service(request: Request):
132132
request_ctx = CatalogRequestContext.create(request)
133133
path_params = parse_request_path_parameters_as(ServicePathParams, request)
@@ -357,7 +357,7 @@ async def get_service_resources(request: Request):
357357
)
358358
@login_required
359359
@permission_required("services.catalog.*")
360-
@reraise_catalog_exceptions_as_http_errors
360+
@handle_plugin_requests_exceptions
361361
async def get_service_pricing_plan(request: Request):
362362
ctx = CatalogRequestContext.create(request)
363363
path_params = parse_request_path_parameters_as(ServicePathParams, request)

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
11
"""Defines the different exceptions that may arise in the catalog subpackage"""
22

3-
import functools
4-
5-
from aiohttp import web
6-
from servicelib.aiohttp.typing_extension import Handler
3+
from servicelib.aiohttp import status
74
from servicelib.rabbitmq.rpc_interfaces.catalog.errors import (
85
CatalogForbiddenError,
96
CatalogItemNotFoundError,
107
)
118

129
from ..errors import WebServerBaseError
10+
from ..exception_handling import (
11+
ExceptionToHttpErrorMap,
12+
HttpErrorInfo,
13+
exception_handling_decorator,
14+
to_exceptions_handlers_map,
15+
)
1316
from ..resource_usage.errors import DefaultPricingPlanNotFoundError
1417

1518

16-
def reraise_catalog_exceptions_as_http_errors(handler: Handler):
17-
@functools.wraps(handler)
18-
async def _wrapper(request: web.Request) -> web.StreamResponse:
19-
try:
20-
21-
return await handler(request)
22-
23-
except (
24-
CatalogItemNotFoundError,
25-
DefaultPricingPlanNotFoundError,
26-
DefaultPricingUnitForServiceNotFoundError,
27-
) as exc:
28-
raise web.HTTPNotFound(reason=f"{exc}") from exc
29-
30-
except CatalogForbiddenError as exc:
31-
raise web.HTTPForbidden(reason=f"{exc}") from exc
32-
33-
return _wrapper
34-
35-
3619
class BaseCatalogError(WebServerBaseError):
3720
msg_template = "Unexpected error occured in catalog submodule"
3821

@@ -60,6 +43,28 @@ def __init__(self, *, service_key: str, service_version: str, **ctxs):
6043
assert CatalogItemNotFoundError # nosec
6144

6245

46+
_TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = {
47+
CatalogItemNotFoundError: HttpErrorInfo(
48+
status.HTTP_404_NOT_FOUND,
49+
"Catalog item not found",
50+
),
51+
DefaultPricingPlanNotFoundError: HttpErrorInfo(
52+
status.HTTP_404_NOT_FOUND,
53+
"Default pricing plan not found",
54+
),
55+
DefaultPricingUnitForServiceNotFoundError: HttpErrorInfo(
56+
status.HTTP_404_NOT_FOUND, "Default pricing unit not found"
57+
),
58+
CatalogForbiddenError: HttpErrorInfo(
59+
status.HTTP_403_FORBIDDEN, "Forbidden catalog access"
60+
),
61+
}
62+
63+
handle_plugin_requests_exceptions = exception_handling_decorator(
64+
to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP)
65+
)
66+
67+
6368
__all__: tuple[str, ...] = (
6469
"CatalogForbiddenError",
6570
"CatalogItemNotFoundError",

0 commit comments

Comments
 (0)