From f4a250f6f54a1b749b0c1085f8bf2524a0d2cc50 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..171a44c06 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 is not None else "?", method, url, - e.response.text, + e.response.text if e.response is not None else "?", ) else: lgr.exception("HTTP connection failed") diff --git a/dandi/dandiarchive.py b/dandi/dandiarchive.py index 97c86ce5b..ebbd6d96b 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 is not None + 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 is not None + 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 73635da9d..e98c67ab6 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 is not None + 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..b8cfe38b1 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 is not None 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..34199793d 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 is not None and "Zarr already exists" in e.response.text: lgr.warning( "%s: Found pre-existing Zarr at same path not" " associated with any asset; reusing",