Skip to content

Commit ef44ed4

Browse files
authored
Merge pull request #1336 from dandi/bf-typing
typing: Account for the fact that requests.HTTPError .response migth be None now
2 parents b575ef5 + f4a250f commit ef44ed4

5 files changed

Lines changed: 22 additions & 10 deletions

File tree

dandi/dandiapi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ def request(
248248
if isinstance(e, requests.HTTPError):
249249
lgr.error(
250250
"HTTP request failed repeatedly: Error %d while sending %s request to %s: %s",
251-
e.response.status_code,
251+
e.response.status_code if e.response is not None else "?",
252252
method,
253253
url,
254-
e.response.text,
254+
e.response.text if e.response is not None else "?",
255255
)
256256
else:
257257
lgr.exception("HTTP connection failed")

dandi/dandiarchive.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ def navigate(
165165
try:
166166
dandiset = self.get_dandiset(client, lazy=not strict)
167167
except requests.HTTPError as e:
168-
if e.response.status_code == 401 and authenticate is not False:
168+
if (
169+
e.response is not None
170+
and e.response.status_code == 401
171+
and authenticate is not False
172+
):
169173
lgr.info("Resource requires authentication; authenticating ...")
170174
client.dandi_authenticate()
171175
dandiset = self.get_dandiset(client, lazy=not strict)
@@ -293,7 +297,11 @@ def navigate(
293297
try:
294298
assets = list(self.get_assets(client, strict=strict))
295299
except requests.HTTPError as e:
296-
if e.response.status_code == 401 and authenticate is not False:
300+
if (
301+
e.response is not None
302+
and e.response.status_code == 401
303+
and authenticate is not False
304+
):
297305
lgr.info("Resource requires authentication; authenticating ...")
298306
client.dandi_authenticate()
299307
assets = list(self.get_assets(client, strict=strict))

dandi/download.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,14 @@ def _download_file(
700700
except requests.exceptions.HTTPError as exc:
701701
# TODO: actually we should probably retry only on selected codes, and also
702702
# respect Retry-After
703-
if attempt >= 2 or exc.response.status_code not in (
704-
400, # Bad Request, but happened with gider:
705-
# https://github.com/dandi/dandi-cli/issues/87
706-
*RETRY_STATUSES,
703+
if attempt >= 2 or (
704+
exc.response is not None
705+
and exc.response.status_code
706+
not in (
707+
400, # Bad Request, but happened with gider:
708+
# https://github.com/dandi/dandi-cli/issues/87
709+
*RETRY_STATUSES,
710+
)
707711
):
708712
lgr.debug("Download failed: %s", exc)
709713
yield {"status": "error", "message": str(exc)}

dandi/files/bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def iter_upload(
362362
},
363363
)
364364
except requests.HTTPError as e:
365-
if e.response.status_code == 409:
365+
if e.response is not None and e.response.status_code == 409:
366366
lgr.debug("%s: Blob already exists on server", asset_path)
367367
blob_id = e.response.headers["Location"]
368368
else:

dandi/files/zarr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def mkzarr() -> str:
299299
json={"name": asset_path, "dandiset": dandiset.identifier},
300300
)
301301
except requests.HTTPError as e:
302-
if "Zarr already exists" in e.response.text:
302+
if e.response is not None and "Zarr already exists" in e.response.text:
303303
lgr.warning(
304304
"%s: Found pre-existing Zarr at same path not"
305305
" associated with any asset; reusing",

0 commit comments

Comments
 (0)