Skip to content

Commit 717934b

Browse files
committed
handle file not found in datcore
1 parent a557632 commit 717934b

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

services/storage/src/simcore_service_storage/datcore_dsm.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
import arrow
6+
from aws_library.s3._errors import S3KeyNotFoundError
67
from fastapi import FastAPI
78
from models_library.api_schemas_storage.storage_schemas import (
89
DatCoreCollectionName,
@@ -31,6 +32,7 @@
3132
from .modules.datcore_adapter import datcore_adapter
3233
from .modules.datcore_adapter.datcore_adapter_exceptions import (
3334
DatcoreAdapterMultipleFilesError,
35+
DatcoreAdapterResponseError,
3436
)
3537
from .modules.db.tokens import get_api_token_and_secret
3638

@@ -191,6 +193,15 @@ async def compute_path_total_size(self, user_id: UserID, *, path: Path) -> ByteS
191193
"""returns the total size of an arbitrary path"""
192194
api_token, api_secret = await self._get_datcore_tokens(user_id)
193195
api_token, api_secret = _check_api_credentials(api_token, api_secret)
196+
try:
197+
paths = await self.list_paths(
198+
user_id, file_filter=path, cursor=None, limit=1
199+
)
200+
if len(paths) == 0:
201+
return ByteSize(0)
202+
except ValidationError:
203+
# invalid path
204+
return ByteSize(0)
194205
raise NotImplementedError
195206

196207
async def list_files(
@@ -268,9 +279,13 @@ async def create_file_download_link(
268279
) -> AnyUrl:
269280
api_token, api_secret = await self._get_datcore_tokens(user_id)
270281
api_token, api_secret = _check_api_credentials(api_token, api_secret)
271-
return await datcore_adapter.get_file_download_presigned_link(
272-
self.app, api_token, api_secret, file_id
273-
)
282+
try:
283+
return await datcore_adapter.get_file_download_presigned_link(
284+
self.app, api_token, api_secret, file_id
285+
)
286+
except DatcoreAdapterResponseError as exc:
287+
if exc.status == status.HTTP_404_NOT_FOUND:
288+
raise S3KeyNotFoundError(key=file_id, bucket=self.buck)
274289

275290
async def delete_file(self, user_id: UserID, file_id: StorageFileID) -> None:
276291
api_token, api_secret = await self._get_datcore_tokens(user_id)

services/storage/src/simcore_service_storage/exceptions/handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from ..modules.datcore_adapter.datcore_adapter_exceptions import (
12+
DatcoreAdapterFileNotFoundError,
1213
DatcoreAdapterTimeoutError,
1314
)
1415
from .errors import (
@@ -42,6 +43,7 @@ def set_exception_handlers(app: FastAPI) -> None:
4243
FileMetaDataNotFoundError,
4344
S3KeyNotFoundError,
4445
ProjectNotFoundError,
46+
DatcoreAdapterFileNotFoundError,
4547
):
4648
app.add_exception_handler(
4749
exc_not_found,

services/storage/src/simcore_service_storage/modules/datcore_adapter/datcore_adapter.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any, TypeAlias, cast
33

44
import httpx
5-
from fastapi import FastAPI
5+
from fastapi import FastAPI, status
66
from fastapi_pagination import Page
77
from models_library.api_schemas_datcore_adapter.datasets import (
88
DatasetMetaData as DatCoreDatasetMetaData,
@@ -33,7 +33,11 @@
3333
TotalNumber,
3434
)
3535
from .datcore_adapter_client_utils import request, retrieve_all_pages
36-
from .datcore_adapter_exceptions import DatcoreAdapterError
36+
from .datcore_adapter_exceptions import (
37+
DatcoreAdapterError,
38+
DatcoreAdapterFileNotFoundError,
39+
DatcoreAdapterResponseError,
40+
)
3741
from .utils import (
3842
create_path_meta_data_from_datcore_fmd,
3943
create_path_meta_data_from_datcore_package,
@@ -316,11 +320,16 @@ async def list_datasets(
316320
async def get_file_download_presigned_link(
317321
app: FastAPI, api_key: str, api_secret: str, file_id: str
318322
) -> AnyUrl:
319-
file_download_data = cast(
320-
dict[str, Any],
321-
await request(app, api_key, api_secret, "GET", f"/files/{file_id}"),
322-
)
323-
return TypeAdapter(AnyUrl).validate_python(file_download_data["link"])
323+
try:
324+
file_download_data = cast(
325+
dict[str, Any],
326+
await request(app, api_key, api_secret, "GET", f"/files/{file_id}"),
327+
)
328+
return TypeAdapter(AnyUrl).validate_python(file_download_data["link"])
329+
except DatcoreAdapterResponseError as exc:
330+
if exc.status == status.HTTP_404_NOT_FOUND:
331+
raise DatcoreAdapterFileNotFoundError(file_id=file_id) from exc
332+
raise
324333

325334

326335
async def get_package_files(

services/storage/src/simcore_service_storage/modules/datcore_adapter/datcore_adapter_exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ def __init__(self, status: int, reason: str) -> None:
4444
super().__init__(
4545
msg=f"forwarded call failed with status {status}, reason {reason}"
4646
)
47+
48+
49+
class DatcoreAdapterFileNotFoundError(DatcoreAdapterError):
50+
"""special error to check the assumption that /packages/{package_id}/files returns only one file"""
51+
52+
def __init__(self, file_id: str) -> None:
53+
self.file_id = file_id
54+
super().__init__(msg=f"file {file_id} not found!")

services/storage/tests/unit/test_handlers_paths.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,6 @@ async def test_path_compute_size_inexistent_path(
740740
faker: Faker,
741741
fake_datcore_tokens: tuple[str, str],
742742
):
743-
744743
await _assert_compute_path_total_size(
745744
initialized_app,
746745
client,

0 commit comments

Comments
 (0)