Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Add `onError` to `subscribe` in `@marimo-team/client` (#11412)

Trevor Manz33738ab243a1
diff --git a/packages/client/REDUCERS.md b/packages/client/REDUCERS.md
index c98f26ccda..49a0861a85 100644
--- a/packages/client/REDUCERS.md
+++ b/packages/client/REDUCERS.md
@@ -108,9 +108,9 @@ function useNotebooks(client: Client) {
```
Keep the client instance stable across renders. Temporary disconnects preserve
-state while the stream resumes. A `reset` rebuilds it. `events.closed` rejects
-if the stream stops with an error, so you can use it to surface connection
-failures in your application.
+state while the stream resumes. A `reset` rebuilds it. Pass a second function
+to `subscribe` to surface connection failures in your application. It
+receives the error when the stream stops.
If you persist state for a later connection, save its matching cursor and
reuse the same host, stream, and filter. The stream's cursor tracks received
diff --git a/packages/client/src/__tests__/notebook-events.test.ts b/packages/client/src/__tests__/notebook-events.test.ts
index 0bfba9ce21..ec9b26acdf 100644
--- a/packages/client/src/__tests__/notebook-events.test.ts
+++ b/packages/client/src/__tests__/notebook-events.test.ts
@@ -357,6 +357,29 @@ describe("NotebookEvents", () => {
expect(events.status).toBe("closed");
});
+ it("tells subscribers why it stopped, unless they left or closed it", async () => {
+ const refused = new Response(null, { status: 403 });
+ const stopped = new NotebookEvents(url, {
+ fetch: answering(refused).fetch,
+ });
+ const subscribed = vi.fn();
+ const left = vi.fn();
+ stopped.subscribe(() => undefined, subscribed);
+ stopped.subscribe(() => undefined, left)();
+ await stopped.closed.catch(() => undefined);
+ const late = vi.fn();
+ stopped.subscribe(() => undefined, late);
+ const closed = new NotebookEvents(url, { fetch: answering().fetch });
+ const closing = vi.fn();
+ closed.subscribe(() => undefined, closing);
+ closed.close();
+ await closed.closed;
+ expect(subscribed.mock.calls).toEqual([[expect.any(StreamStopped)]]);
+ expect(left).not.toHaveBeenCalled();
+ expect(late).not.toHaveBeenCalled();
+ expect(closing).not.toHaveBeenCalled();
+ });
+
it.each([
'event: cell\ndata: {"cell":{"id":"c1"}}\n\n',
"event: reset\ndata: 5\n\n",
diff --git a/packages/client/src/transport/events.ts b/packages/client/src/transport/events.ts
index a960572328..eb0ace9caf 100644
--- a/packages/client/src/transport/events.ts
+++ b/packages/client/src/transport/events.ts
@@ -76,6 +76,7 @@ export class Events<
readonly #onStatus: ((status: Status) => void) | undefined;
#settle: { resolve(): void; reject(error: unknown): void } | undefined;
#release: (() => void) | undefined;
+ readonly #onErrors = new Set<(error: unknown) => void>();
public constructor(
source: string | URL | Transport,
@@ -133,13 +134,26 @@ export class Events<
}
/**
- * Call `listener` with each event, in order.
+ * Call `onEvent` with each event, in order, and `onError` if the stream stops.
*
- * Returns a function that stops calling it. A listener that throws does not
+ * `onError` receives the error `closed` rejects with. Closing the stream does
+ * not call it, and neither does a stream that stopped before subscribing.
+ * Returns a function that stops calling both. A callback that throws does not
* interrupt the stream.
*/
- public subscribe(listener: (event: StreamEvent<K>) => void): () => void {
- return this.#stream.subscribe(listener);
+ public subscribe(
+ onEvent: (event: StreamEvent<K>) => void,
+ onError?: (error: unknown) => void,
+ ): () => void {
+ const unsubscribe = this.#stream.subscribe(onEvent);
+ if (onError === undefined || this.#settle === undefined) {
+ return unsubscribe;
+ }
+ this.#onErrors.add(onError);
+ return () => {
+ this.#onErrors.delete(onError);
+ unsubscribe();
+ };
}
/** Stop following the stream. */
@@ -261,16 +275,34 @@ export class Events<
}
}
- /** Close the stream once and return how to settle `closed`. */
+ /**
+ * Close the stream once and return how to settle `closed`.
+ *
+ * Rejecting also calls each subscriber's `onError`.
+ */
#finish() {
const settle = this.#settle;
if (settle === undefined) {
return undefined;
}
this.#settle = undefined;
+ const onErrors = [...this.#onErrors];
+ this.#onErrors.clear();
this.#release?.();
this.#controller.abort();
this.#setStatus("closed");
- return settle;
+ return {
+ resolve: settle.resolve,
+ reject: (error: unknown) => {
+ settle.reject(error);
+ for (const onError of onErrors) {
+ try {
+ onError(error);
+ } catch (thrown) {
+ report(thrown);
+ }
+ }
+ },
+ };
}
}