Skip to content

Commit

Permalink
refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Neagu committed Aug 21, 2024
1 parent f7c9fc1 commit f6902d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import re
import uuid
from collections.abc import Iterable
Expand All @@ -17,6 +16,7 @@
Field,
NonNegativeInt,
PositiveInt,
ValidationError,
parse_obj_as,
)
from pydantic.errors import PydanticErrorMixin
Expand All @@ -31,8 +31,6 @@
from .models.groups import GroupType, groups
from .utils_ordering import OrderDirection

_logger = logging.getLogger(__name__)

_ProductName: TypeAlias = str
_ProjectID: TypeAlias = uuid.UUID
_GroupID: TypeAlias = PositiveInt
Expand Down Expand Up @@ -530,7 +528,10 @@ async def folder_create(
GroupIdDoesNotExistError
RootFolderRequiresAtLeastOnePrimaryGroupError
"""
parse_obj_as(FolderName, name)
try:
parse_obj_as(FolderName, name)
except ValidationError as exc:
raise InvalidFolderNameError(name=name, reason=f"{exc}") from exc

async with connection.begin():
entry_exists: int | None = await connection.scalar(
Expand Down
4 changes: 2 additions & 2 deletions packages/postgres-database/tests/test_utils_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sqlalchemy as sa
from aiopg.sa.connection import SAConnection
from aiopg.sa.result import RowProxy
from pydantic import BaseModel, Field, NonNegativeInt, ValidationError
from pydantic import BaseModel, Field, NonNegativeInt
from pytest_simcore.helpers.faker_factories import random_product
from simcore_postgres_database.models.folders import (
folders,
Expand Down Expand Up @@ -191,7 +191,7 @@ async def default_product_name(
],
)
async def test_folder_create_wrong_folder_name(invalid_name: str):
with pytest.raises((InvalidFolderNameError, ValidationError)):
with pytest.raises(InvalidFolderNameError):
await folder_create(Mock(), "mock_product", invalid_name, Mock())


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
from servicelib.request_keys import RQT_USERID_KEY
from servicelib.rest_constants import RESPONSE_MODEL_POLICY
from simcore_postgres_database.utils_folders import FoldersError

from .._constants import RQ_PRODUCT_KEY
from .._meta import API_VTAG as VTAG
Expand All @@ -51,6 +52,9 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
except FolderAccessForbiddenError as exc:
raise web.HTTPForbidden(reason=f"{exc}") from exc

except FoldersError as exc:
raise web.HTTPBadRequest(reason=f"{exc}") from exc

return wrapper


Expand Down

0 comments on commit f6902d3

Please sign in to comment.