Skip to content
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

Handling uncaught runtime error in promise when executing code in output #302

Merged
merged 6 commits into from
Sep 9, 2024
Merged
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
11 changes: 7 additions & 4 deletions packages/react/src/components/output/Output.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
/*
* Copyright (c) 2021-2023 Datalayer, Inc.
*
Expand Down Expand Up @@ -40,7 +41,8 @@ export type IOutputProps = {
showEditor: boolean;
showKernelProgressBar?: boolean;
toolbarPosition: 'up' | 'middle' | 'none';
notifyOnComplete? : boolean;
notifyOnComplete?: boolean;
onCodeExecutionError?: (err : any) => void;
};

export const Output = (props: IOutputProps) => {
Expand All @@ -64,6 +66,7 @@ export const Output = (props: IOutputProps) => {
showEditor,
showKernelProgressBar = true,
notifyOnComplete = false,
onCodeExecutionError,
id: sourceId,
toolbarPosition,
} = props;
Expand Down Expand Up @@ -123,7 +126,7 @@ export const Output = (props: IOutputProps) => {
useEffect(() => {
if (adapter) {
if (autoRun) {
adapter.execute(code, notifyOnComplete);
adapter.execute(code, notifyOnComplete,onCodeExecutionError);
}
}
}, [adapter]);
Expand All @@ -143,12 +146,12 @@ export const Output = (props: IOutputProps) => {
const executeRequest = outputStore.getExecuteRequest(sourceId);
useEffect(() => {
if (adapter && executeRequest && executeRequest === id) {
adapter.execute(code, notifyOnComplete);
adapter.execute(code, notifyOnComplete,onCodeExecutionError);
}
}, [executeRequest, adapter]);
useEffect(() => {
if (adapter && executeTrigger > 0) {
adapter.execute(code, notifyOnComplete);
adapter.execute(code, notifyOnComplete,onCodeExecutionError);
}
}, [executeTrigger]);
useEffect(() => {
Expand Down
42 changes: 35 additions & 7 deletions packages/react/src/components/output/OutputAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@

import { IOutput } from '@jupyterlab/nbformat';
import { JSONObject } from '@lumino/coreutils';
import { IOutputAreaModel, OutputArea, OutputAreaModel } from '@jupyterlab/outputarea';
import { IRenderMime, RenderMimeRegistry, standardRendererFactories } from '@jupyterlab/rendermime';
import {
IOutputAreaModel,
OutputArea,
OutputAreaModel,
} from '@jupyterlab/outputarea';
import {
IRenderMime,
RenderMimeRegistry,
standardRendererFactories,
} from '@jupyterlab/rendermime';
import { rendererFactory as jsonRendererFactory } from '@jupyterlab/json-extension';
import { rendererFactory as javascriptRendererFactory } from '@jupyterlab/javascript-extension';
import { WIDGET_MIMETYPE, WidgetRenderer } from '@jupyter-widgets/html-manager/lib/output_renderers';
import {
WIDGET_MIMETYPE,
WidgetRenderer,
} from '@jupyter-widgets/html-manager/lib/output_renderers';
import { requireLoader as loader } from '../../jupyter/ipywidgets/libembed-amd';
import { ClassicWidgetManager } from '../../jupyter/ipywidgets/classic/manager';
import { Kernel } from '../../jupyter/kernel/Kernel';
Expand Down Expand Up @@ -80,12 +91,29 @@ export class OutputAdapter {
this.initKernel();
}

public async execute(code: string, notifyOnComplete? : boolean) {
public async execute(
code: string,
notifyOnComplete?: boolean,
onCodeExecutionError?: (err: any) => void
) {
if (this._kernel) {
this.clear();
const metadata : JSONObject = {};
const done = execute(this._id, code, this._outputArea, this._kernel, metadata, notifyOnComplete);
await done;
const metadata: JSONObject = {};
const done = execute(
this._id,
code,
this._outputArea,
this._kernel,
metadata,
notifyOnComplete
);
if (onCodeExecutionError) {
await done.catch(onCodeExecutionError);
} else {
await done.catch(err => {
console.error(err);
});
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions packages/react/src/components/output/OutputExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function execute(
output: OutputArea,
kernel: Kernel,
metadata?: JSONObject,
notifyOnComplete? : boolean
notifyOnComplete?: boolean
): Promise<KernelMessage.IExecuteReplyMsg | undefined> {
// Override the default for `stop_on_error`.
let stopOnError = true;
Expand All @@ -29,14 +29,11 @@ export async function execute(
) {
stopOnError = false;
}
const kernelExecutor = kernel.execute(
code,
{
model: output.model,
stopOnError,
notifyOnComplete : notifyOnComplete
}
);
const kernelExecutor = kernel.execute(code, {
model: output.model,
stopOnError,
notifyOnComplete: notifyOnComplete,
});
const future = kernelExecutor!.future;
// TODO fix in upstream jupyterlab if possible...
(output as any)._onIOPub = future!.onIOPub;
Expand Down
Loading