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
  • Loading branch information
Anita Caron committed Dec 7, 2024
1 parent 3d7d3b0 commit 3a31a0e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 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,18 @@ 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)
# Exit the function if the URL does not exist
return
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 3a31a0e

Please sign in to comment.