From f61f6b8e4e691267f2a1b6b7f36ac86d626c8798 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Mon, 16 Oct 2023 14:13:45 -0400 Subject: [PATCH] typing: Account for the fact that requests.HTTPError .response migth be None now --- dandi/dandiapi.py | 4 ++-- dandi/dandiarchive.py | 12 ++++++++++-- dandi/download.py | 12 ++++++++---- dandi/files/bases.py | 2 +- dandi/files/zarr.py | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/dandi/dandiapi.py b/dandi/dandiapi.py index 435cd9a55..eb024511f 100644 --- a/dandi/dandiapi.py +++ b/dandi/dandiapi.py @@ -248,10 +248,10 @@ def request( if isinstance(e, requests.HTTPError): lgr.error( "HTTP request failed repeatedly: Error %d while sending %s request to %s: %s", - e.response.status_code, + e.response.status_code if e.response else "?", method, url, - e.response.text, + e.response.text if e.response else "?", ) else: lgr.exception("HTTP connection failed") diff --git a/dandi/dandiarchive.py b/dandi/dandiarchive.py index 68cea6d05..6d835d6ba 100644 --- a/dandi/dandiarchive.py +++ b/dandi/dandiarchive.py @@ -165,7 +165,11 @@ def navigate( try: dandiset = self.get_dandiset(client, lazy=not strict) except requests.HTTPError as e: - if e.response.status_code == 401 and authenticate is not False: + if ( + e.response + and e.response.status_code == 401 + and authenticate is not False + ): lgr.info("Resource requires authentication; authenticating ...") client.dandi_authenticate() dandiset = self.get_dandiset(client, lazy=not strict) @@ -293,7 +297,11 @@ def navigate( try: assets = list(self.get_assets(client, strict=strict)) except requests.HTTPError as e: - if e.response.status_code == 401 and authenticate is not False: + if ( + e.response + and e.response.status_code == 401 + and authenticate is not False + ): lgr.info("Resource requires authentication; authenticating ...") client.dandi_authenticate() assets = list(self.get_assets(client, strict=strict)) diff --git a/dandi/download.py b/dandi/download.py index aad1af22e..b8b846870 100644 --- a/dandi/download.py +++ b/dandi/download.py @@ -700,10 +700,14 @@ def _download_file( except requests.exceptions.HTTPError as exc: # TODO: actually we should probably retry only on selected codes, and also # respect Retry-After - if attempt >= 2 or exc.response.status_code not in ( - 400, # Bad Request, but happened with gider: - # https://github.com/dandi/dandi-cli/issues/87 - *RETRY_STATUSES, + if attempt >= 2 or ( + exc.response + and exc.response.status_code + not in ( + 400, # Bad Request, but happened with gider: + # https://github.com/dandi/dandi-cli/issues/87 + *RETRY_STATUSES, + ) ): lgr.debug("Download failed: %s", exc) yield {"status": "error", "message": str(exc)} diff --git a/dandi/files/bases.py b/dandi/files/bases.py index 83e4f01f1..a919b7d54 100644 --- a/dandi/files/bases.py +++ b/dandi/files/bases.py @@ -362,7 +362,7 @@ def iter_upload( }, ) except requests.HTTPError as e: - if e.response.status_code == 409: + if e.response and e.response.status_code == 409: lgr.debug("%s: Blob already exists on server", asset_path) blob_id = e.response.headers["Location"] else: diff --git a/dandi/files/zarr.py b/dandi/files/zarr.py index 1a06dd553..ca13ec965 100644 --- a/dandi/files/zarr.py +++ b/dandi/files/zarr.py @@ -299,7 +299,7 @@ def mkzarr() -> str: json={"name": asset_path, "dandiset": dandiset.identifier}, ) except requests.HTTPError as e: - if "Zarr already exists" in e.response.text: + if e.response and "Zarr already exists" in e.response.text: lgr.warning( "%s: Found pre-existing Zarr at same path not" " associated with any asset; reusing",