Skip to content

Commit 841cf3b

Browse files
committed
fixup! Add option to expose connection errors in httpfs.exists
1 parent db97bd1 commit 841cf3b

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

fsspec/implementations/http.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,13 @@ async def _exists(self, path, strict=False, **kwargs):
334334
session = await self.set_session()
335335
r = await session.get(self.encode_url(path), **kw)
336336
async with r:
337+
if strict:
338+
self._raise_not_found_for_status(r, path)
337339
return r.status < 400
338-
except aiohttp.ClientError:
339-
if strict:
340+
except (FileNotFoundError, aiohttp.ClientError) as e:
341+
if strict and isinstance(e, FileNotFoundError):
342+
return False
343+
elif strict:
340344
raise
341345
else:
342346
return False

fsspec/implementations/http_sync.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,22 @@ def _process_limits(self, url, start, end):
463463
end -= 1 # bytes range is inclusive
464464
return f"bytes={start}-{end}"
465465

466-
def exists(self, path, **kwargs):
466+
def exists(self, path, strict=False, **kwargs):
467467
kw = self.kwargs.copy()
468468
kw.update(kwargs)
469469
try:
470470
logger.debug(path)
471471
r = self.session.get(self.encode_url(path), **kw)
472+
if strict:
473+
self._raise_not_found_for_status(r, path)
472474
return r.status_code < 400
473-
except Exception:
474-
return False
475+
except Exception as e:
476+
if strict and isinstance(e, FileNotFoundError):
477+
return False
478+
elif strict:
479+
raise
480+
else:
481+
return False
475482

476483
def isfile(self, path, **kwargs):
477484
return self.exists(path, **kwargs)

fsspec/implementations/tests/test_http.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ def test_exists(server):
163163
h.cat(server.address + "/notafile")
164164

165165

166+
def test_exists_strict(server):
167+
h = fsspec.filesystem("http")
168+
assert not h.exists(server.address + "/notafile", strict=True)
169+
with pytest.raises(aiohttp.ClientResponseError) as e:
170+
h.exists(server.address + "/unauthorized", strict=True)
171+
assert e.value.status == 401
172+
173+
166174
def test_read(server):
167175
h = fsspec.filesystem("http")
168176
out = server.realfile

fsspec/implementations/tests/test_http_sync.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
import fsspec.utils
9-
from fsspec.tests.conftest import data, reset_files, server, win # noqa: F401
9+
from fsspec.tests.conftest import data, reset_files, server, win, requests # noqa: F401
1010

1111

1212
@pytest.fixture()
@@ -147,6 +147,14 @@ def test_exists(server, sync):
147147
h.cat(server.address + "/notafile")
148148

149149

150+
def test_exists_strict(server, sync):
151+
h = fsspec.filesystem("http")
152+
assert not h.exists(server.address + "/notafile", strict=True)
153+
with pytest.raises(requests.exceptions.HTTPError) as e:
154+
h.exists(server.address + "/unauthorized", strict=True)
155+
assert e.value.response.status_code == 401
156+
157+
150158
def test_read(server, sync):
151159
h = fsspec.filesystem("http")
152160
out = server.address + "/index/realfile"

fsspec/tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class HTTPTestHandler(BaseHTTPRequestHandler):
5454
"/simple/file": data,
5555
"/simple/dir/": _make_listing("/simple/dir/file"),
5656
"/simple/dir/file": data,
57+
"/unauthorized": AssertionError("shouldn't access"),
5758
}
5859
dynamic_files = {}
5960

@@ -85,6 +86,8 @@ def do_GET(self):
8586
if "redirect" in self.headers and file_path != "/index/realfile":
8687
new_url = _make_realfile(baseurl)
8788
return self._respond(301, {"Location": new_url})
89+
if file_path == "/unauthorized":
90+
return self._respond(401)
8891
if file_data is None:
8992
return self._respond(404)
9093

0 commit comments

Comments
 (0)