Skip to content

Commit

Permalink
More robust error catching
Browse files Browse the repository at this point in the history
  • Loading branch information
teutoburg committed Aug 26, 2024
1 parent dbd3471 commit 9fcc30d
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions scopesim/server/download_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ def handle_download(
"""
tqdm_kwargs = _make_tqdm_kwargs(f"Downloading {name:<{padlen}}")
save_path = Path(save_path)
save_path.parent.mkdir(parents=True, exist_ok=True)
stream = send_get(client, sub_url, True, params)

try:
with send_get(client, sub_url, True, params) as response:
with stream as response:
response.raise_for_status()
total = int(response.headers.get("Content-Length", 0))

with (Path(save_path).open("wb") as file_outer,
with (save_path.open("wb") as file_outer,
tqdm.wrapattr(
file_outer, "write", miniters=1, total=total,
**tqdm_kwargs, disable=disable_bar) as file_inner):
Expand All @@ -134,6 +137,8 @@ def handle_download(
except OSError as err:
logger.error("Error %s attempting to access path %s.", err, save_path)
raise ServerError("Cannot access save path.") from err
except ServerError:
raise

Check warning on line 141 in scopesim/server/download_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/download_utils.py#L137-L141

Added lines #L137 - L141 were not covered by tests
except Exception as err:
logger.exception("Unhandled exception while accessing server.")
raise ServerError("Cannot perform download.") from err

Check warning on line 144 in scopesim/server/download_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/download_utils.py#L144

Added line #L144 was not covered by tests
Expand Down

0 comments on commit 9fcc30d

Please sign in to comment.