diff --git a/packages/postgres-database/src/simcore_postgres_database/utils_folders.py b/packages/postgres-database/src/simcore_postgres_database/utils_folders.py index 15ae70993fc..e0f59cdcfd2 100644 --- a/packages/postgres-database/src/simcore_postgres_database/utils_folders.py +++ b/packages/postgres-database/src/simcore_postgres_database/utils_folders.py @@ -1,4 +1,3 @@ -import logging import re import uuid from collections.abc import Iterable @@ -17,6 +16,7 @@ Field, NonNegativeInt, PositiveInt, + ValidationError, parse_obj_as, ) from pydantic.errors import PydanticErrorMixin @@ -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 @@ -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( diff --git a/packages/postgres-database/tests/test_utils_folders.py b/packages/postgres-database/tests/test_utils_folders.py index 9bfc1d4f04e..8c49fd9914f 100644 --- a/packages/postgres-database/tests/test_utils_folders.py +++ b/packages/postgres-database/tests/test_utils_folders.py @@ -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, @@ -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()) diff --git a/services/web/server/src/simcore_service_webserver/folders/_folders_handlers.py b/services/web/server/src/simcore_service_webserver/folders/_folders_handlers.py index ed8f6108df6..1e67d43c6d8 100644 --- a/services/web/server/src/simcore_service_webserver/folders/_folders_handlers.py +++ b/services/web/server/src/simcore_service_webserver/folders/_folders_handlers.py @@ -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 @@ -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