Skip to content

Commit d22f250

Browse files
committed
Decouple download error handling again
1 parent 79a36cf commit d22f250

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

src/ghga_connector/core/downloading/batch_processing.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _handle_failures(self) -> bool:
194194
return True
195195

196196
async def manage_file_downloads(self, overwrite: bool) -> AsyncGenerator[FileInfo]:
197-
"""Manages file downloads by handling errors, checking for existing files,
197+
"""Manages file downloads by checking for existing files before download,
198198
printing messages to the display, and renaming files after they are downloaded.
199199
200200
Yields file information.
@@ -203,19 +203,7 @@ async def manage_file_downloads(self, overwrite: bool) -> AsyncGenerator[FileInf
203203
staged_files = await self.get_staged_files()
204204
for file_info in staged_files:
205205
utils.check_for_existing_file(file_info=file_info, overwrite=overwrite)
206-
try:
207-
file_id = file_info.file_id
208-
yield file_info
209-
except exceptions.GetEnvelopeError as error:
210-
CLIMessageDisplay.failure(
211-
f"The request to get an envelope for file '{file_id}' failed."
212-
)
213-
raise error
214-
except exceptions.DownloadError as error:
215-
CLIMessageDisplay.failure(
216-
f"Failed downloading with id '{file_id}'."
217-
)
218-
raise error
206+
yield file_info
219207
file_info.path_during_download.rename(file_info.path_once_complete)
220208
CLIMessageDisplay.success(
221209
f"File with id '{file_info.file_id}' has been successfully downloaded."

src/ghga_connector/core/downloading/downloader.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import gc
2020
import logging
2121
from asyncio import PriorityQueue, Queue, Semaphore, Task
22+
from contextlib import contextmanager
2223
from io import BufferedWriter
2324
from pathlib import Path
2425

@@ -28,11 +29,29 @@
2829

2930
from ..progress_bar import DownloadProgressBar
3031
from .api_calls import DownloadClient, extract_download_url
31-
from .structs import RetryResponse
32+
from .structs import FileInfo, RetryResponse
3233

3334
logger = logging.getLogger(__name__)
3435
# TODO: [later] More better logging
3536

37+
__all__ = ["Downloader", "handle_download_errors"]
38+
39+
40+
@contextmanager
41+
def handle_download_errors(file_info: FileInfo):
42+
"""Used to handle download errors from `Downloader.download_file()`"""
43+
file_id = file_info.file_id
44+
try:
45+
yield
46+
except exceptions.GetEnvelopeError as error:
47+
CLIMessageDisplay.failure(
48+
f"The request to get an envelope for file '{file_id}' failed."
49+
)
50+
raise error
51+
except exceptions.DownloadError as error:
52+
CLIMessageDisplay.failure(f"Failed downloading with id '{file_id}'.")
53+
raise error
54+
3655

3756
class Downloader:
3857
"""Centralized high-level interface for downloading a single file.

src/ghga_connector/core/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
from ghga_connector.core.client import async_client
2525
from ghga_connector.core.downloading.api_calls import DownloadClient
2626
from ghga_connector.core.downloading.batch_processing import FileStager
27-
from ghga_connector.core.downloading.downloader import Downloader
27+
from ghga_connector.core.downloading.downloader import (
28+
Downloader,
29+
handle_download_errors,
30+
)
2831
from ghga_connector.core.work_package import WorkPackageClient
2932

3033
from .. import exceptions
@@ -153,9 +156,11 @@ async def async_download(
153156
max_concurrent_downloads=CONFIG.max_concurrent_downloads,
154157
)
155158
CLIMessageDisplay.display(f"Downloading file with id '{file_id}'...")
156-
await downloader.download_file(
157-
output_path=file_info.path_during_download, part_size=CONFIG.part_size
158-
)
159+
with handle_download_errors(file_info):
160+
await downloader.download_file(
161+
output_path=file_info.path_during_download,
162+
part_size=CONFIG.part_size,
163+
)
159164

160165

161166
def decrypt_file(

0 commit comments

Comments
 (0)