Skip to content

Commit

Permalink
Enhance error handling in download_file function to include HTTPError…
Browse files Browse the repository at this point in the history
… and RequestException (#150)

* Enhance error handling in download_file function to include HTTPError and RequestException

* Add return value to indicate download status

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: Anita Caron <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 7, 2024
1 parent 3d7d3b0 commit fae1819
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions util/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

import requests
import yaml
from requests.exceptions import ChunkedEncodingError
from requests.exceptions import (ChunkedEncodingError, HTTPError,
RequestException)

obo_purl = "http://purl.obolibrary.org/obo/"

Expand Down Expand Up @@ -594,8 +595,17 @@ def download_file(url, dest_path, retries=3):
f.write(chunk)
logging.info("Downloaded %s to %s", url, dest_path)
return # Exit the function if download is successful
except ChunkedEncodingError as e:
attempt += 1
logging.warning("ChunkedEncodingError encountered: %s. Retrying %s/%s...", e, attempt, retries)
except Exception as e:
except HTTPError as e:
logging.exception("Failed to download %s: %s", url, e)
return False
except ChunkedEncodingError as e:
logging.warning(
"ChunkedEncodingError encountered: %s. Retrying %s/%s...",
e, attempt + 1, retries
)
except RequestException as e:
logging.warning(
"Failed to download %s: %s (%s). Retrying %s/%s...",
url, e, type(e).__name__, attempt + 1, retries
)
attempt += 1

0 comments on commit fae1819

Please sign in to comment.