Skip to content

Commit

Permalink
feat: error message for failed upload (#99)
Browse files Browse the repository at this point in the history
* feat: error message for failed upload

* fix: test returns exceptions

* fix

* fix: format

* chore: bump version
  • Loading branch information
ArzelaAscoIi authored Sep 4, 2023
1 parent 2c65f94 commit 7b947ec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion deepset_cloud_sdk/__about__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""This file defines the package version."""

__version__ = "0.0.28"
__version__ = "0.0.29"
31 changes: 16 additions & 15 deletions deepset_cloud_sdk/_s3/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ def __init__(self, error: aiohttp.ClientResponseError) -> None:
self.error = error


@dataclass
class S3UploadSummary:
"""A summary of the S3 upload results."""

total_files: int
successful_upload_count: int
failed_upload_count: int
failed: List[str]


@dataclass
class S3UploadResult:
"""Stores the result of an upload to S3."""
Expand All @@ -51,6 +41,16 @@ class S3UploadResult:
exception: Optional[Exception] = None


@dataclass
class S3UploadSummary:
"""A summary of the S3 upload results."""

total_files: int
successful_upload_count: int
failed_upload_count: int
failed: List[S3UploadResult]


def make_safe_file_name(file_name: str) -> str:
"""
Transform a given string to a representation that S3 accepts.
Expand Down Expand Up @@ -164,13 +164,14 @@ async def upload_from_file(
try:
await self._upload_file_with_retries(file_name, upload_session, content, client_session)
return S3UploadResult(file_name=file_name, success=True)
except: # pylint: disable=bare-except
logger.warn(
except Exception as exception: # pylint: disable=broad-exception-caught
logger.error(
"Could not upload a file to deepset Cloud",
file_name=file_name,
session_id=upload_session.session_id,
exception=str(exception),
)
return S3UploadResult(file_name=file_name, success=False)
return S3UploadResult(file_name=file_name, success=False, exception=exception)

async def upload_from_string(
self,
Expand Down Expand Up @@ -220,13 +221,13 @@ async def _process_results(
failed_files=[r for r in results if not r.success],
)

failed: List[str] = []
failed: List[S3UploadResult] = []
successfully_uploaded = 0
for result in results:
if result.success:
successfully_uploaded += 1
else:
failed.append(result.file_name)
failed.append(result)

result_summary = S3UploadSummary(
successful_upload_count=successfully_uploaded,
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/s3/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ async def test_upload_files_from_path_http_error(self, upload_session_response:
assert results.successful_upload_count == 0
assert results.failed_upload_count == 6
assert len(results.failed) == 6
assert results.failed == [
assert [f.file_name for f in results.failed] == [
"16675.txt",
"16675.txt.meta.json",
"22297.txt",
"22297.txt.meta.json",
"35887.txt",
"35887.txt.meta.json",
]
assert all(isinstance(f.exception, RetryableHttpError) for f in results.failed)

async def test_upload_texts_http_error(self, upload_session_response: UploadSession) -> None:
exception = aiohttp.ClientResponseError(request_info=Mock(), history=Mock(), status=503)
Expand All @@ -138,11 +139,13 @@ async def test_upload_texts_http_error(self, upload_session_response: UploadSess
assert results.successful_upload_count == 0
assert results.failed_upload_count == 3
assert len(results.failed) == 3
assert results.failed == [

assert [f.file_name for f in results.failed] == [
"one.txt",
"two.txt",
"three.txt",
]
assert all(isinstance(f.exception, RetryableHttpError) for f in results.failed)

async def test_upload_texts_with_metadata_http_error(self, upload_session_response: UploadSession) -> None:
exception = aiohttp.ClientResponseError(request_info=Mock(), history=Mock(), status=503)
Expand All @@ -160,14 +163,15 @@ async def test_upload_texts_with_metadata_http_error(self, upload_session_respon
assert results.successful_upload_count == 0
assert results.failed_upload_count == 6
assert len(results.failed) == 6
assert results.failed == [
assert [f.file_name for f in results.failed] == [
"one.txt",
"one.txt.meta.json",
"two.txt",
"two.txt.meta.json",
"three.txt",
"three.txt.meta.json",
]
assert all(isinstance(f.exception, RetryableHttpError) for f in results.failed)

@pytest.mark.parametrize("status", [503, 502, 500, 504, 408])
@patch("aiohttp.ClientSession")
Expand Down

0 comments on commit 7b947ec

Please sign in to comment.