Skip to content

Commit 158e9d1

Browse files
authored
Merge pull request #649 from intake/fix/http-ls
Raise FileNotFoundError from HTTP.ls
2 parents 8ea35ab + 2ad392e commit 158e9d1

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

fsspec/implementations/http.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async def _ls_real(self, url, detail=True, **kwargs):
138138
logger.debug(url)
139139
session = await self.set_session()
140140
async with session.get(url, **self.kwargs) as r:
141-
r.raise_for_status()
141+
self._raise_not_found_for_status(r, url)
142142
text = await r.text()
143143
if self.simple_links:
144144
links = ex2.findall(text) + [u[2] for u in ex.findall(text)]
@@ -190,6 +190,14 @@ async def _ls(self, url, detail=True, **kwargs):
190190

191191
ls = sync_wrapper(_ls)
192192

193+
def _raise_not_found_for_status(self, response, url):
194+
"""
195+
Raises FileNotFoundError for 404s, otherwise uses raise_for_status.
196+
"""
197+
if response.status == 404:
198+
raise FileNotFoundError(url)
199+
response.raise_for_status()
200+
193201
async def _cat_file(self, url, start=None, end=None, **kwargs):
194202
kw = self.kwargs.copy()
195203
kw.update(kwargs)
@@ -225,9 +233,7 @@ async def _cat_file(self, url, start=None, end=None, **kwargs):
225233
kw["headers"] = headers
226234
session = await self.set_session()
227235
async with session.get(url, **kw) as r:
228-
if r.status == 404:
229-
raise FileNotFoundError(url)
230-
r.raise_for_status()
236+
self._raise_not_found_for_status(r, url)
231237
out = await r.read()
232238
return out
233239

@@ -237,9 +243,7 @@ async def _get_file(self, rpath, lpath, chunk_size=5 * 2 ** 20, **kwargs):
237243
logger.debug(rpath)
238244
session = await self.set_session()
239245
async with session.get(rpath, **self.kwargs) as r:
240-
if r.status == 404:
241-
raise FileNotFoundError(rpath)
242-
r.raise_for_status()
246+
self._raise_not_found_for_status(r, rpath)
243247
with open(lpath, "wb") as fd:
244248
chunk = True
245249
while chunk:

fsspec/implementations/tests/test_http.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ def test_list_cache_reuse(server):
201201
assert len(h.dircache) == 0
202202

203203

204+
def test_ls_raises_filenotfound(server):
205+
h = fsspec.filesystem("http")
206+
207+
with pytest.raises(FileNotFoundError):
208+
h.ls(server + "/not-a-key")
209+
210+
204211
def test_list_cache_with_max_paths(server):
205212
h = fsspec.filesystem("http", use_listings_cache=True, max_paths=5)
206213
out = h.glob(server + "/index/*")

0 commit comments

Comments
 (0)