Skip to content

Commit 110e1eb

Browse files
committed
improves error handling
1 parent e84b6d2 commit 110e1eb

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

services/web/server/src/simcore_service_webserver/exceptions_handlers.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
22
import logging
3-
from typing import NamedTuple, TypeAlias
3+
from typing import Iterable, NamedTuple, TypeAlias
44

55
from aiohttp import web
66
from servicelib.aiohttp.typing_extension import Handler
@@ -25,7 +25,7 @@ def __missing__(self, key):
2525

2626

2727
def _sort_exceptions_by_specificity(
28-
exceptions: list[type[BaseException]], *, concrete_first: bool = True
28+
exceptions: Iterable[type[BaseException]], *, concrete_first: bool = True
2929
) -> list[type[BaseException]]:
3030
return sorted(
3131
exceptions,
@@ -35,22 +35,27 @@ def _sort_exceptions_by_specificity(
3535

3636

3737
def create_exception_handlers_decorator(
38-
exception_catch: type[BaseException] | tuple[type[BaseException], ...],
38+
exceptions_catch: type[BaseException] | tuple[type[BaseException], ...],
3939
exc_to_status_map: ExceptionToHttpErrorMap,
4040
):
41-
42-
included: list[type[BaseException]] = _sort_exceptions_by_specificity(
43-
list(exc_to_status_map.keys())
41+
mapped_classes: tuple[type[BaseException], ...] = tuple(
42+
_sort_exceptions_by_specificity(exc_to_status_map.keys())
4443
)
4544

45+
assert all( # nosec
46+
issubclass(cls, exceptions_catch) for cls in mapped_classes
47+
), f"Every {mapped_classes=} must inherit by one or more of {exceptions_catch=}"
48+
4649
def _decorator(handler: Handler):
4750
@functools.wraps(handler)
4851
async def _wrapper(request: web.Request) -> web.StreamResponse:
4952
try:
5053
return await handler(request)
5154

52-
except exception_catch as exc:
53-
if exc_cls := next((_ for _ in included if isinstance(exc, _)), None):
55+
except exceptions_catch as exc:
56+
if exc_cls := next(
57+
(cls for cls in mapped_classes if isinstance(exc, cls)), None
58+
):
5459
http_error_info = exc_to_status_map[exc_cls]
5560

5661
# safe formatting, i.e. does not raise
@@ -77,7 +82,7 @@ async def _wrapper(request: web.Request) -> web.StreamResponse:
7782
)
7883
)
7984
raise http_error_cls(reason=user_msg) from exc
80-
raise
85+
raise # reraise
8186

8287
return _wrapper
8388

services/web/server/src/simcore_service_webserver/folders/_exceptions_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@
6969

7070

7171
handle_plugin_requests_exceptions = create_exception_handlers_decorator(
72-
exception_catch=(BaseProjectError, FoldersValueError, WorkspacesValueError),
72+
exceptions_catch=(BaseProjectError, FoldersValueError, WorkspacesValueError),
7373
exc_to_status_map=_TO_HTTP_ERROR_MAP,
7474
)

services/web/server/src/simcore_service_webserver/projects/_trash_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747

4848
_handle_exceptions = create_exception_handlers_decorator(
49-
exception_catch=ProjectTrashError, exc_to_status_map=_TO_HTTP_ERROR_MAP
49+
exceptions_catch=ProjectTrashError, exc_to_status_map=_TO_HTTP_ERROR_MAP
5050
)
5151

5252
#

services/web/server/tests/unit/isolated/test_exceptions_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def test_exception_handlers_decorator(
7070
):
7171

7272
_handle_exceptions = create_exception_handlers_decorator(
73-
exception_catch=BasePluginError,
73+
exceptions_catch=BasePluginError,
7474
exc_to_status_map={
7575
OneError: HttpErrorInfo(
7676
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,

0 commit comments

Comments
 (0)