Skip to content

Commit

Permalink
raise FileNotFoundError if file isn't found
Browse files Browse the repository at this point in the history
  • Loading branch information
d70-t committed Feb 28, 2022
1 parent f02f92e commit 4f11240
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion swiftspec/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ async def _ls(self, path, detail=True, **kwargs):

ls = sync_wrapper(_ls)

def _raise_not_found_for_status(self, response, ref):
"""
Raises FileNotFoundError for 404s, otherwise uses raise_for_status.
"""
if response.status == 404:
raise FileNotFoundError(ref.swift_url)
response.raise_for_status()

async def _cat_file(self, path, start=None, end=None, **kwargs):
ref = SWIFTRef(path)
headers = self.headers_for_url(ref.http_url)
Expand All @@ -228,7 +236,7 @@ async def _cat_file(self, path, start=None, end=None, **kwargs):

session = await self.set_session()
async with session.get(ref.http_url, headers=headers) as res:
res.raise_for_status()
self._raise_not_found_for_status(res, ref)
return await res.read()

async def _pipe_file(self, path, data, chunksize=50 * 2 ** 20, **kwargs):
Expand Down
5 changes: 5 additions & 0 deletions test/test_swiftfilesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ def test_cat(fs):
assert fs.cat("swift://server/a1/c1/hello") == b"Hello World"


def test_cat_raises_not_found(fs):
with pytest.raises(FileNotFoundError):
assert fs.cat("swift://server/a1/c1/doesnt_exist")


def test_cat_partial(fs):
assert fs.cat("swift://server/a1/c1/hello", start=3, end=5) == b"lo"

Expand Down

0 comments on commit 4f11240

Please sign in to comment.