From 625539c786c72fdfc25f424410ce674c5be32111 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Thu, 30 Sep 2021 15:37:06 -0600 Subject: [PATCH] Avoid crash when downloading files --- scripts/wk/kit/tools.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/scripts/wk/kit/tools.py b/scripts/wk/kit/tools.py index 50a98059..da3e69c4 100644 --- a/scripts/wk/kit/tools.py +++ b/scripts/wk/kit/tools.py @@ -27,7 +27,9 @@ CACHED_DIRS = {} def download_file(out_path, source_url, as_new=False, overwrite=False): """Download a file using requests, returns pathlib.Path.""" out_path = pathlib.Path(out_path).resolve() - download_msg = f'Downloading {out_path.name}...' + name = out_path.name + download_failed = None + download_msg = f'Downloading {name}...' if as_new: out_path = out_path.with_suffix(f'{out_path.suffix}.new') print(download_msg, end='', flush=True) @@ -40,15 +42,17 @@ def download_file(out_path, source_url, as_new=False, overwrite=False): out_path.parent.mkdir(parents=True, exist_ok=True) # Request download - response = requests.get(source_url, stream=True) - if not response.ok: - name = out_path.name - if as_new: - name = name[:-4] - LOG.error( - 'Failed to download file (status %s): %s', - response.status_code, name, - ) + try: + response = requests.get(source_url, stream=True) + except requests.RequestException as _err: + download_failed = _err + else: + if not response.ok: + download_failed = response + + # Download failed + if download_failed: + LOG.error('Failed to download file: %s', download_failed) raise GenericError(f'Failed to download file: {name}') # Write to file