Skip to content

Commit ce83e3f

Browse files
authored
adlfs: raise nicer FileNotFoundError's (fsspec#444)
1 parent af8e6e8 commit ce83e3f

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

adlfs/spec.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,9 @@ async def _info(self, path, refresh=False, **kwargs):
576576
) as cc:
577577
properties = await cc.get_container_properties()
578578
except ResourceNotFoundError as exc:
579-
raise FileNotFoundError from exc
579+
raise FileNotFoundError(
580+
errno.ENOENT, "No such container", container
581+
) from exc
580582
info = (await self._details([properties]))[0]
581583
# Make result consistent with _ls_containers()
582584
if not info.get("metadata"):
@@ -1384,15 +1386,15 @@ async def _pipe(self, *args, batch_size=None, max_concurrency=None, **kwargs):
13841386
async def _cat_file(
13851387
self, path, start=None, end=None, max_concurrency=None, **kwargs
13861388
):
1387-
path = self._strip_protocol(path)
1389+
stripped_path = self._strip_protocol(path)
13881390
if end is not None:
13891391
start = start or 0 # download_blob requires start if length is provided.
13901392
length = end - start
13911393
else:
13921394
length = None
1393-
container_name, path, version_id = self.split_path(path)
1395+
container_name, blob, version_id = self.split_path(stripped_path)
13941396
async with self.service_client.get_blob_client(
1395-
container=container_name, blob=path
1397+
container=container_name, blob=blob
13961398
) as bc:
13971399
try:
13981400
stream = await bc.download_blob(
@@ -1403,10 +1405,14 @@ async def _cat_file(
14031405
**self._timeout_kwargs,
14041406
)
14051407
except ResourceNotFoundError as e:
1406-
raise FileNotFoundError from e
1408+
raise FileNotFoundError(
1409+
errno.ENOENT, os.strerror(errno.ENOENT), path
1410+
) from e
14071411
except HttpResponseError as e:
14081412
if version_id is not None:
1409-
raise FileNotFoundError from e
1413+
raise FileNotFoundError(
1414+
errno.ENOENT, os.strerror(errno.ENOENT), path
1415+
) from e
14101416
raise
14111417
result = await stream.readall()
14121418
return result
@@ -1586,7 +1592,9 @@ async def _put_file(
15861592
raise FileExistsError("File already exists!")
15871593
except ResourceNotFoundError:
15881594
if not await self._exists(container_name):
1589-
raise FileNotFoundError("Container does not exist.")
1595+
raise FileNotFoundError(
1596+
errno.ENOENT, "No such container", container_name
1597+
)
15901598
await self._put_file(lpath, rpath, delimiter, overwrite)
15911599
self.invalidate_cache()
15921600

@@ -1600,16 +1608,16 @@ async def _put(self, *args, batch_size=None, max_concurrency=None, **kwargs):
16001608

16011609
async def _cp_file(self, path1, path2, **kwargs):
16021610
"""Copy the file at path1 to path2"""
1603-
container1, path1, version_id = self.split_path(path1, delimiter="/")
1604-
container2, path2, _ = self.split_path(path2, delimiter="/")
1611+
container1, blob1, version_id = self.split_path(path1, delimiter="/")
1612+
container2, blob2, _ = self.split_path(path2, delimiter="/")
16051613

16061614
cc1 = self.service_client.get_container_client(container1)
1607-
blobclient1 = cc1.get_blob_client(blob=path1)
1615+
blobclient1 = cc1.get_blob_client(blob=blob1)
16081616
if container1 == container2:
1609-
blobclient2 = cc1.get_blob_client(blob=path2)
1617+
blobclient2 = cc1.get_blob_client(blob=blob2)
16101618
else:
16111619
cc2 = self.service_client.get_container_client(container2)
1612-
blobclient2 = cc2.get_blob_client(blob=path2)
1620+
blobclient2 = cc2.get_blob_client(blob=blob2)
16131621
url = (
16141622
blobclient1.url
16151623
if version_id is None
@@ -1618,7 +1626,9 @@ async def _cp_file(self, path1, path2, **kwargs):
16181626
try:
16191627
await blobclient2.start_copy_from_url(url)
16201628
except ResourceNotFoundError as e:
1621-
raise FileNotFoundError from e
1629+
raise FileNotFoundError(
1630+
errno.ENOENT, os.strerror(errno.ENOENT), path1
1631+
) from e
16221632
self.invalidate_cache(container1)
16231633
self.invalidate_cache(container2)
16241634

@@ -1661,7 +1671,9 @@ async def _get_file(
16611671
with open(lpath, "wb") as my_blob:
16621672
await stream.readinto(my_blob)
16631673
except ResourceNotFoundError as exception:
1664-
raise FileNotFoundError from exception
1674+
raise FileNotFoundError(
1675+
errno.ENOENT, os.strerror(errno.ENOENT), rpath
1676+
) from exception
16651677

16661678
get_file = sync_wrapper(_get_file)
16671679

@@ -1682,7 +1694,9 @@ async def _setxattrs(self, rpath, **kwargs):
16821694
await bc.set_blob_metadata(metadata=kwargs)
16831695
self.invalidate_cache(self._parent(rpath))
16841696
except Exception as e:
1685-
raise FileNotFoundError(f"File not found for {e}")
1697+
raise FileNotFoundError(
1698+
errno.ENOENT, os.strerror(errno.ENOENT), rpath
1699+
) from e
16861700

16871701
setxattrs = sync_wrapper(_setxattrs)
16881702

0 commit comments

Comments
 (0)