Skip to content

Commit e8ab936

Browse files
committed
revisit exceptions
1 parent 4878f49 commit e8ab936

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

ipfsspec/async_ipfs.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async def info(self, path, session):
8181
node = unixfsv1.PBNode.loads(resdata)
8282
data = unixfsv1.Data.loads(node.Data)
8383
if data.Type == unixfsv1.DataType.Raw:
84-
raise ValueError(f"The path '{path}' is only a subsection of a file")
84+
raise FileNotFoundError(path) # this is not a file, it's only a part of it
8585
elif data.Type == unixfsv1.DataType.Directory:
8686
return {
8787
"name": path,
@@ -109,14 +109,12 @@ async def info(self, path, session):
109109
elif data.Type == unixfsv1.DataType.HAMTShard:
110110
raise NotImplementedError(f"The path '{path}' contains a HAMTSharded directory, this is currently not implemented")
111111
else:
112-
raise ValueError(f"The path '{path}' is neiter an IPFS UNIXFSv1 object")
112+
raise FileNotFoundError(path) # it exists, but is not a UNIXFSv1 object, so it's not a file
113113

114114
async def cat(self, path, session):
115115
res = await self.get(path, session)
116116
async with res:
117117
self._raise_not_found_for_status(res, path)
118-
if res.status != 200:
119-
raise FileNotFoundError(path)
120118
return await res.read()
121119

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

134132
if detail:
135133
return await asyncio.gather(*(
@@ -142,9 +140,9 @@ def _raise_not_found_for_status(self, response, url):
142140
"""
143141
Raises FileNotFoundError for 404s, otherwise uses raise_for_status.
144142
"""
145-
if response.status == 404:
143+
if response.status == 404: # returned for known missing files
146144
raise FileNotFoundError(url)
147-
elif response.status == 400:
145+
elif response.status == 400: # return for invalid requests, so it's also certainly not there
148146
raise FileNotFoundError(url)
149147
response.raise_for_status()
150148

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

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

0 commit comments

Comments
 (0)