Skip to content

Commit 4484f12

Browse files
Fix FileNotFoundError handling in rename_file methods (#1548)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5b0aed9 commit 4484f12

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

jupyter_server/services/contents/filemanager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ def rename_file(self, old_path, new_path):
602602
shutil.move(old_os_path, new_os_path)
603603
except web.HTTPError:
604604
raise
605+
except FileNotFoundError:
606+
raise web.HTTPError(404, f"File or directory does not exist: {old_path}") from None
605607
except Exception as e:
606608
raise web.HTTPError(500, f"Unknown error renaming file: {old_path} {e}") from e
607609

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

tests/services/contents/test_manager.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,21 @@ async def test_rename(jp_contents_manager):
854854
await ensure_async(cm.new_untitled("foo/bar_diff", ext=".ipynb"))
855855

856856

857+
async def test_rename_nonexistent(jp_contents_manager):
858+
"""Test renaming a non-existent file/directory returns 404 error"""
859+
cm = jp_contents_manager
860+
861+
# Test with non-existent file
862+
with pytest.raises(HTTPError) as e:
863+
await ensure_async(cm.rename("nonexistent_file.txt", "new_name.txt"))
864+
assert expected_http_error(e, 404)
865+
866+
# Test with non-existent directory
867+
with pytest.raises(HTTPError) as e:
868+
await ensure_async(cm.rename("nonexistent_dir", "new_dir"))
869+
assert expected_http_error(e, 404)
870+
871+
857872
async def test_delete_root(jp_contents_manager):
858873
cm = jp_contents_manager
859874
with pytest.raises(HTTPError) as e:

0 commit comments

Comments
 (0)