Skip to content

Commit b99f3c5

Browse files
authored
Propagate Error to API (#44)
* add-error-in-results * add-test
1 parent 36154de commit b99f3c5

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

jupyter_server_nbmodel/actions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ async def kernel_worker(
279279
queue.task_done()
280280
get_logger().debug(f"Execution request {uid} processed for kernel {kernel_id}.")
281281
except (asyncio.CancelledError, KeyboardInterrupt, RuntimeError) as e:
282+
results[uid] = {"error": str(e)}
282283
get_logger().debug(
283284
f"Stopping execution requests worker for kernel {kernel_id}…", exc_info=e
284285
)

jupyter_server_nbmodel/tests/test_handlers.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
import pytest
1212
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
13+
from jupyter_client.asynchronous.client import AsyncKernelClient
14+
from jupyter_server_nbmodel.models import PendingInput
15+
from jupyter_server_nbmodel.actions import kernel_worker
1316

1417
TEST_TIMEOUT = 15
1518

@@ -154,6 +157,44 @@ async def test_post_erroneous_execute(jp_fetch, pending_kernel_is_ready, snippet
154157

155158
await asyncio.sleep(1)
156159

160+
@pytest.mark.asyncio
161+
async def test_kernel_worker_reports_error(monkeypatch):
162+
# Patch _execute_snippet to raise an error
163+
async def fake_execute(client, ydoc, snippet, metadata, stdin_hook):
164+
raise RuntimeError("simulated failure")
165+
monkeypatch.setattr(
166+
"jupyter_server_nbmodel.actions._execute_snippet",
167+
fake_execute,
168+
)
169+
170+
queue = asyncio.Queue()
171+
results = {}
172+
pending_input = PendingInput()
173+
174+
uid = "test-uid"
175+
snippet = "print('won't run')"
176+
metadata = {"foo": "bar"}
177+
await queue.put((uid, snippet, metadata))
178+
179+
client = AsyncKernelClient()
180+
ydoc = None
181+
182+
worker_task = asyncio.create_task(
183+
kernel_worker("kernel-id", client, ydoc, queue, results, pending_input)
184+
)
185+
186+
await asyncio.sleep(0.05)
187+
188+
# The worker will hit RuntimeError
189+
with pytest.raises(RuntimeError) as excinfo:
190+
await worker_task
191+
192+
assert "simulated failure" in str(excinfo.value)
193+
194+
assert uid in results, "kernel_worker should have recorded an entry for our uid"
195+
assert isinstance(results[uid], dict)
196+
assert "error" in results[uid], f"Expected an 'error' key in results[{uid!r}]"
197+
assert "simulated failure" in results[uid]["error"]
157198

158199
@pytest.mark.timeout(TEST_TIMEOUT)
159200
async def test_execution_timing_metadata(jp_root_dir, jp_fetch, pending_kernel_is_ready, rtc_create_notebook, jp_serverapp):

0 commit comments

Comments
 (0)