Skip to content

Commit 603337e

Browse files
sok82Sergey Kadnikov
andauthored
Clear fix for 301 - handling promise reject inside KernelExecutor (#307)
* Clear fix for 301 - handling promise reject inside KernelExecutor * Fixed bubbling exception if no handler exists for KernelExecutor --------- Co-authored-by: Sergey Kadnikov <skadnikov@seeneco.ru>
1 parent 64c873f commit 603337e

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

packages/react/src/components/output/OutputAdapter.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,10 @@ export class OutputAdapter {
105105
this._outputArea,
106106
this._kernel,
107107
metadata,
108-
notifyOnComplete
108+
notifyOnComplete,
109+
onCodeExecutionError
109110
);
110-
if (onCodeExecutionError) {
111-
await done.catch(onCodeExecutionError);
112-
} else {
113-
await done.catch(err => {
114-
console.error(err);
115-
});
116-
}
111+
await done;
117112
}
118113
}
119114

packages/react/src/components/output/OutputExecutor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export async function execute(
1818
output: OutputArea,
1919
kernel: Kernel,
2020
metadata?: JSONObject,
21-
notifyOnComplete?: boolean
21+
notifyOnComplete?: boolean,
22+
onCodeExecutionError?: (err: any) => void
2223
): Promise<KernelMessage.IExecuteReplyMsg | undefined> {
2324
// Override the default for `stop_on_error`.
2425
let stopOnError = true;
@@ -32,7 +33,8 @@ export async function execute(
3233
const kernelExecutor = kernel.execute(code, {
3334
model: output.model,
3435
stopOnError,
35-
notifyOnComplete: notifyOnComplete,
36+
notifyOnComplete,
37+
onCodeExecutionError,
3638
});
3739
const future = kernelExecutor!.future;
3840
// TODO fix in upstream jupyterlab if possible...

packages/react/src/jupyter/kernel/Kernel.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ export class Kernel {
194194
stopOnError,
195195
storeHistory,
196196
allowStdin,
197-
notifyOnComplete = false
197+
notifyOnComplete = false,
198+
onCodeExecutionError
198199
}: {
199200
model?: IOutputAreaModel;
200201
iopubMessageHooks?: IOPubMessageHook[];
@@ -203,14 +204,16 @@ export class Kernel {
203204
stopOnError?: boolean;
204205
storeHistory?: boolean;
205206
allowStdin?: boolean;
206-
notifyOnComplete? : boolean
207+
notifyOnComplete?: boolean
208+
onCodeExecutionError?: (err:any) => void;
207209
} = {}
208210
): KernelExecutor | undefined {
209211
if (this._kernelConnection) {
210212
const kernelExecutor = new KernelExecutor({
211213
connection: this._kernelConnection,
212214
model,
213215
notifyOnComplete,
216+
onCodeExecutionError
214217
});
215218
kernelExecutor.execute(code, {
216219
iopubMessageHooks,

packages/react/src/jupyter/kernel/KernelExecutor.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ export type IKernelExecutorOptions = {
4646
* must only be made when execution complete
4747
*/
4848
notifyOnComplete? : boolean;
49+
/**
50+
* Handler for code execution error
51+
* @param err Error data
52+
* @returns
53+
*/
54+
onCodeExecutionError?: (err: any) => void;
4955
}
5056

5157
export class KernelExecutor {
@@ -60,14 +66,16 @@ export class KernelExecutor {
6066
private _future?: JupyterKernel.IFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg>;
6167
private _shellMessageHooks = new Array<ShellMessageHook>();
6268
private _notifyOnComplete : boolean = false;
69+
private _onCodeExecutionError?: (err: any) => void;
6370

64-
public constructor({ connection, model, notifyOnComplete }: IKernelExecutorOptions) {
71+
public constructor({ connection, model, notifyOnComplete, onCodeExecutionError }: IKernelExecutorOptions) {
6572
this._executed = new PromiseDelegate<IOutputAreaModel>();
6673
this._kernelConnection = connection;
6774
this._model = model ?? new OutputAreaModel();
6875
this._outputs = [];
6976
this._kernelState = kernelsStore.getState();
7077
this._notifyOnComplete = !!notifyOnComplete;
78+
this._onCodeExecutionError = onCodeExecutionError;
7179
}
7280

7381
/**
@@ -163,7 +171,17 @@ export class KernelExecutor {
163171
get done(): Promise<void> {
164172
return this._executed.promise.then(() => {
165173
return;
166-
});
174+
})
175+
.catch(
176+
(err: any) => {
177+
if (this._onCodeExecutionError) {
178+
this._onCodeExecutionError(err);
179+
}
180+
else {
181+
throw err;
182+
}
183+
}
184+
);
167185
}
168186

169187
/**
@@ -172,7 +190,18 @@ export class KernelExecutor {
172190
get result(): Promise<string> {
173191
return this._executed.promise.then(model => {
174192
return outputsAsString(model.toJSON());
175-
});
193+
})
194+
.catch(
195+
(err: any) => {
196+
if (this._onCodeExecutionError) {
197+
this._onCodeExecutionError(err);
198+
return "";
199+
}
200+
else {
201+
throw err;
202+
}
203+
}
204+
);
176205
}
177206

178207
/**

0 commit comments

Comments
 (0)