Skip to content

Commit

Permalink
revisit exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
d70-t committed Sep 19, 2024
1 parent 4878f49 commit e8ab936
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions ipfsspec/async_ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def info(self, path, session):
node = unixfsv1.PBNode.loads(resdata)
data = unixfsv1.Data.loads(node.Data)
if data.Type == unixfsv1.DataType.Raw:
raise ValueError(f"The path '{path}' is only a subsection of a file")
raise FileNotFoundError(path) # this is not a file, it's only a part of it
elif data.Type == unixfsv1.DataType.Directory:
return {
"name": path,
Expand Down Expand Up @@ -109,14 +109,12 @@ async def info(self, path, session):
elif data.Type == unixfsv1.DataType.HAMTShard:
raise NotImplementedError(f"The path '{path}' contains a HAMTSharded directory, this is currently not implemented")
else:
raise ValueError(f"The path '{path}' is neiter an IPFS UNIXFSv1 object")
raise FileNotFoundError(path) # it exists, but is not a UNIXFSv1 object, so it's not a file

async def cat(self, path, session):
res = await self.get(path, session)
async with res:
self._raise_not_found_for_status(res, path)
if res.status != 200:
raise FileNotFoundError(path)
return await res.read()

async def ls(self, path, session, detail=False):
Expand All @@ -129,7 +127,7 @@ async def ls(self, path, session, detail=False):
data = unixfsv1.Data.loads(node.Data)
if data.Type != unixfsv1.DataType.Directory:
# TODO: we might need support for HAMTShard here (for large directories)
raise ValueError(f"The path '{path}' is not a directory")
raise NotADirectoryError(path)

if detail:
return await asyncio.gather(*(
Expand All @@ -142,9 +140,9 @@ def _raise_not_found_for_status(self, response, url):
"""
Raises FileNotFoundError for 404s, otherwise uses raise_for_status.
"""
if response.status == 404:
if response.status == 404: # returned for known missing files
raise FileNotFoundError(url)
elif response.status == 400:
elif response.status == 400: # return for invalid requests, so it's also certainly not there
raise FileNotFoundError(url)
response.raise_for_status()

Expand Down Expand Up @@ -301,7 +299,7 @@ async def _info(self, path, **kwargs):

def open(self, path, mode="rb", block_size=None, cache_options=None, **kwargs):
if mode != "rb":
raise NotImplementedError
raise NotImplementedError("opening modes other than read binary are not implemented")
data = self.cat_file(path) # load whole chunk into memory
return io.BytesIO(data)

Expand Down

0 comments on commit e8ab936

Please sign in to comment.