Skip to content

Fix FileNotFoundError handling in rename_file methods #1548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ def rename_file(self, old_path, new_path):
shutil.move(old_os_path, new_os_path)
except web.HTTPError:
raise
except FileNotFoundError:
raise web.HTTPError(404, f"File or directory does not exist: {old_path}") from None
except Exception as e:
raise web.HTTPError(500, f"Unknown error renaming file: {old_path} {e}") from e

Expand Down Expand Up @@ -1069,6 +1071,8 @@ async def rename_file(self, old_path, new_path):
await run_sync(shutil.move, old_os_path, new_os_path)
except web.HTTPError:
raise
except FileNotFoundError:
raise web.HTTPError(404, f"File or directory does not exist: {old_path}") from None
except Exception as e:
raise web.HTTPError(500, f"Unknown error renaming file: {old_path} {e}") from e

Expand Down
15 changes: 15 additions & 0 deletions tests/services/contents/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,21 @@ async def test_rename(jp_contents_manager):
await ensure_async(cm.new_untitled("foo/bar_diff", ext=".ipynb"))


async def test_rename_nonexistent(jp_contents_manager):
"""Test renaming a non-existent file/directory returns 404 error"""
cm = jp_contents_manager

# Test with non-existent file
with pytest.raises(HTTPError) as e:
await ensure_async(cm.rename("nonexistent_file.txt", "new_name.txt"))
assert expected_http_error(e, 404)

# Test with non-existent directory
with pytest.raises(HTTPError) as e:
await ensure_async(cm.rename("nonexistent_dir", "new_dir"))
assert expected_http_error(e, 404)


async def test_delete_root(jp_contents_manager):
cm = jp_contents_manager
with pytest.raises(HTTPError) as e:
Expand Down
Loading