|
1 | 1 | """Defines the different exceptions that may arise in the catalog subpackage""" |
2 | 2 |
|
3 | | -import functools |
4 | | - |
5 | | -from aiohttp import web |
6 | | -from servicelib.aiohttp.typing_extension import Handler |
| 3 | +from servicelib.aiohttp import status |
7 | 4 | from servicelib.rabbitmq.rpc_interfaces.catalog.errors import ( |
8 | 5 | CatalogForbiddenError, |
9 | 6 | CatalogItemNotFoundError, |
10 | 7 | ) |
11 | 8 |
|
12 | 9 | from ..errors import WebServerBaseError |
| 10 | +from ..exception_handling import ( |
| 11 | + ExceptionToHttpErrorMap, |
| 12 | + HttpErrorInfo, |
| 13 | + exception_handling_decorator, |
| 14 | + to_exceptions_handlers_map, |
| 15 | +) |
13 | 16 | from ..resource_usage.errors import DefaultPricingPlanNotFoundError |
14 | 17 |
|
15 | 18 |
|
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 | | - |
36 | 19 | class BaseCatalogError(WebServerBaseError): |
37 | 20 | msg_template = "Unexpected error occured in catalog submodule" |
38 | 21 |
|
@@ -60,6 +43,28 @@ def __init__(self, *, service_key: str, service_version: str, **ctxs): |
60 | 43 | assert CatalogItemNotFoundError # nosec |
61 | 44 |
|
62 | 45 |
|
| 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 | + |
63 | 68 | __all__: tuple[str, ...] = ( |
64 | 69 | "CatalogForbiddenError", |
65 | 70 | "CatalogItemNotFoundError", |
|
0 commit comments