Skip to content

Commit 89599e0

Browse files
committed
updates errors
1 parent 4c19070 commit 89599e0

File tree

2 files changed

+31
-26
lines changed
  • packages/postgres-database/src/simcore_postgres_database
  • services/web/server/src/simcore_service_webserver/tags

2 files changed

+31
-26
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_tags.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from typing import TypedDict
55

6+
from common_library.errors_classes import OsparcErrorMixin
67
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
78

89
from .utils_repos import pass_or_acquire_connection, transaction_context
@@ -20,15 +21,15 @@
2021
#
2122
# Errors
2223
#
23-
class BaseTagError(Exception):
24+
class _BaseTagError(ValueError, OsparcErrorMixin):
2425
pass
2526

2627

27-
class TagNotFoundError(BaseTagError):
28+
class TagNotFoundError(_BaseTagError):
2829
pass
2930

3031

31-
class TagOperationNotAllowedError(BaseTagError): # maps to AccessForbidden
32+
class TagOperationNotAllowedError(_BaseTagError): # maps to AccessForbidden
3233
pass
3334

3435

@@ -163,8 +164,7 @@ async def get(
163164
result = await conn.execute(stmt_get)
164165
row = result.first()
165166
if not row:
166-
msg = f"{tag_id=} not found: either no access or does not exists"
167-
raise TagNotFoundError(msg)
167+
raise TagNotFoundError(operation="get", tag_id=tag_id, user_id=user_id)
168168
return TagDict(
169169
id=row.id,
170170
name=row.name,
@@ -198,8 +198,9 @@ async def update(
198198
result = await conn.execute(update_stmt)
199199
row = result.first()
200200
if not row:
201-
msg = f"{tag_id=} not updated: either no access or not found"
202-
raise TagOperationNotAllowedError(msg)
201+
raise TagOperationNotAllowedError(
202+
operation="update", tag_id=tag_id, user_id=user_id
203+
)
203204

204205
return TagDict(
205206
id=row.id,
@@ -222,8 +223,9 @@ async def delete(
222223
async with transaction_context(self.engine, connection) as conn:
223224
deleted = await conn.scalar(stmt_delete)
224225
if not deleted:
225-
msg = f"Could not delete {tag_id=}. Not found or insuficient access."
226-
raise TagOperationNotAllowedError(msg)
226+
raise TagOperationNotAllowedError(
227+
operation="delete", tag_id=tag_id, user_id=user_id
228+
)
227229

228230
#
229231
# ACCESS RIGHTS

services/web/server/src/simcore_service_webserver/tags/_rest.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import functools
2-
31
from aiohttp import web
42
from pydantic import TypeAdapter
53
from servicelib.aiohttp import status
64
from servicelib.aiohttp.requests_validation import (
75
parse_request_body_as,
86
parse_request_path_parameters_as,
97
)
10-
from servicelib.aiohttp.typing_extension import Handler
118
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
129
from simcore_postgres_database.utils_tags import (
1310
TagNotFoundError,
1411
TagOperationNotAllowedError,
1512
)
1613

1714
from .._meta import API_VTAG as VTAG
15+
from ..exception_handling import (
16+
ExceptionToHttpErrorMap,
17+
HttpErrorInfo,
18+
exception_handling_decorator,
19+
to_exceptions_handlers_map,
20+
)
1821
from ..login.decorators import login_required
1922
from ..security.decorators import permission_required
2023
from ..utils_aiohttp import envelope_json_response
@@ -29,20 +32,20 @@
2932
TagUpdate,
3033
)
3134

32-
33-
def _handle_tags_exceptions(handler: Handler):
34-
@functools.wraps(handler)
35-
async def wrapper(request: web.Request) -> web.StreamResponse:
36-
try:
37-
return await handler(request)
38-
39-
except TagNotFoundError as exc:
40-
raise web.HTTPNotFound(reason=f"{exc}") from exc
41-
42-
except TagOperationNotAllowedError as exc:
43-
raise web.HTTPForbidden(reason=f"{exc}") from exc
44-
45-
return wrapper
35+
_TO_HTTP_ERROR_MAP: ExceptionToHttpErrorMap = {
36+
TagNotFoundError: HttpErrorInfo(
37+
status.HTTP_404_NOT_FOUND,
38+
"Tag {tag_id} not found: either no access or does not exists",
39+
),
40+
TagOperationNotAllowedError: HttpErrorInfo(
41+
status.HTTP_403_FORBIDDEN,
42+
"Could not {operation} tag {tag_id}. Not found or insuficient access.",
43+
),
44+
}
45+
46+
_handle_tags_exceptions = exception_handling_decorator(
47+
to_exceptions_handlers_map(_TO_HTTP_ERROR_MAP)
48+
)
4649

4750

4851
routes = web.RouteTableDef()

0 commit comments

Comments
 (0)