Skip to content

Commit

Permalink
fix: skip gracefully when virtual file is not found (#3241)
Browse files Browse the repository at this point in the history
It's possible the file is not longer in the virtual memory, but this is
ok as the export should be valid.

Fixes #3235
  • Loading branch information
mscolnick authored Dec 19, 2024
1 parent d7c0e43 commit f4e3bf5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions marimo/_server/export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,16 @@ def export_as_html(
for filename_and_length in request.files:
if filename_and_length.startswith("/@file/"):
filename = filename_and_length[7:]
byte_length, basename = filename.split("-", 1)
buffer_contents = read_virtual_file(basename, int(byte_length))
try:
byte_length, basename = filename.split("-", 1)
buffer_contents = read_virtual_file(basename, int(byte_length))
except Exception as e:
LOGGER.warning(
"File not found in export: %s. Error: %s",
filename_and_length,
e,
)
continue
mime_type, _ = mimetypes.guess_type(basename) or (
"text/plain",
None,
Expand Down
18 changes: 18 additions & 0 deletions tests/_server/api/endpoints/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ def test_export_html_no_code(client: TestClient) -> None:
assert CODE not in body


@with_session(SESSION_ID)
def test_export_html_file_not_found(client: TestClient) -> None:
session = get_session_manager(client).get_session(SESSION_ID)
assert session
session.app_file_manager.filename = "test.py"
response = client.post(
"/api/export/html",
headers=HEADERS,
json={
"download": False,
"files": ["/test-10.csv"],
"include_code": True,
},
)
assert response.status_code == 200
assert "<marimo-code hidden=" in response.text


# Read session forces empty code
@with_read_session(SESSION_ID)
def test_export_html_no_code_in_read(client: TestClient) -> None:
Expand Down

0 comments on commit f4e3bf5

Please sign in to comment.