Skip to content

Commit 87a83b2

Browse files
sok82Sergey Kadnikov
andauthored
Fix for 301 - early registering of catch in promise for KernelExecutor (#309)
* Fix for 301 - early registering of catch in promise for KernelExecutor * 301 - Added OutputWithErrorHandle example --------- Co-authored-by: Sergey Kadnikov <skadnikov@seeneco.ru>
1 parent edeec8b commit 87a83b2

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

packages/react/src/examples/Output.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
* MIT License
55
*/
66

7-
import { useEffect, useState } from 'react';
8-
import { createRoot } from 'react-dom/client';
97
import { IOutput } from '@jupyterlab/nbformat';
108
import { Box, Text } from '@primer/react';
11-
import { useOutputsStore } from './../components/output/OutputState';
12-
import { useJupyter } from '../jupyter/JupyterContext';
9+
import { useEffect, useState } from 'react';
10+
import { createRoot } from 'react-dom/client';
11+
import { KernelIndicator } from '../components/kernel/Kernelndicator';
12+
import { Output } from '../components/output/Output';
1313
import { Jupyter } from '../jupyter/Jupyter';
14+
import { useJupyter } from '../jupyter/JupyterContext';
1415
import { Kernel } from '../jupyter/kernel/Kernel';
1516
import { useKernelsStore } from '../jupyter/kernel/KernelState';
16-
import { KernelIndicator } from '../components/kernel/Kernelndicator';
1717
import { newUuid } from '../utils/Utils';
18-
import { Output } from '../components/output/Output';
18+
import { useOutputsStore } from './../components/output/OutputState';
1919

2020
const SOURCE_ID_1 = 'output-id-1';
2121
const OUTPUTS_1: IOutput[] = [
@@ -62,6 +62,7 @@ const OutputWithoutEditor = () => {
6262
outputStore.getModel(SOURCE_ID_1)?.toJSON(),
6363
outputStore.getInput(SOURCE_ID_1),
6464
);
65+
6566
return (
6667
<>
6768
<Text as="h1">Output without Code Editor</Text>
@@ -78,11 +79,13 @@ const OutputWithoutEditor = () => {
7879
const OutputWithEditor = () => {
7980
const { defaultKernel } = useJupyter();
8081
const outputStore = useOutputsStore();
82+
8183
console.log(
8284
'Outputs 2',
8385
outputStore.getModel(SOURCE_ID_2)?.toJSON(),
8486
outputStore.getInput(SOURCE_ID_2),
8587
);
88+
8689
return (
8790
<>
8891
<Text as="h1">Output with Code Editor</Text>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2021-2023 Datalayer, Inc.
3+
*
4+
* MIT License
5+
*/
6+
7+
import { IOutput } from '@jupyterlab/nbformat';
8+
import { Button, Text } from '@primer/react';
9+
import { useState } from 'react';
10+
import { createRoot } from 'react-dom/client';
11+
import { Output } from '../components/output/Output';
12+
import { useOutputsStore } from '../components/output/OutputState';
13+
import { Jupyter } from '../jupyter/Jupyter';
14+
import { useJupyter } from '../jupyter/JupyterContext';
15+
16+
const SOURCE_ID = 'output-id-2';
17+
const SOURCE = 'fail';
18+
19+
20+
const OutputWithEditor = () => {
21+
const { defaultKernel } = useJupyter();
22+
const outputStore = useOutputsStore();
23+
const [execTrigger, setExecTrigger] = useState(0);
24+
25+
console.log(
26+
'Outputs',
27+
outputStore.getModel(SOURCE_ID)?.toJSON(),
28+
outputStore.getInput(SOURCE_ID),
29+
);
30+
31+
const handleExecutionError = (err : any) => {
32+
alert('Execution error - ' + err);
33+
}
34+
35+
return (
36+
<>
37+
<Text as="h1">Output with Error Handling</Text>
38+
<Button onClick={() => setExecTrigger(execTrigger => execTrigger+1)}>Execute 'fail' code</Button>
39+
<Output
40+
autoRun={false}
41+
code={SOURCE}
42+
id={SOURCE_ID}
43+
kernel={defaultKernel}
44+
showEditor={false}
45+
executeTrigger={execTrigger}
46+
onCodeExecutionError={handleExecutionError}
47+
/>
48+
</>
49+
);
50+
};
51+
52+
const div = document.createElement('div');
53+
document.body.appendChild(div);
54+
const root = createRoot(div);
55+
56+
root.render(
57+
<Jupyter>
58+
<OutputWithEditor />
59+
</Jupyter>
60+
);

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,6 @@ export class KernelExecutor {
172172
return this._executed.promise.then(() => {
173173
return;
174174
})
175-
.catch(
176-
(err: any) => {
177-
if (this._onCodeExecutionError) {
178-
this._onCodeExecutionError(err);
179-
}
180-
else {
181-
throw err;
182-
}
183-
}
184-
);
185175
}
186176

187177
/**
@@ -191,17 +181,6 @@ export class KernelExecutor {
191181
return this._executed.promise.then(model => {
192182
return outputsAsString(model.toJSON());
193183
})
194-
.catch(
195-
(err: any) => {
196-
if (this._onCodeExecutionError) {
197-
this._onCodeExecutionError(err);
198-
return "";
199-
}
200-
else {
201-
throw err;
202-
}
203-
}
204-
);
205184
}
206185

207186
/**
@@ -305,6 +284,11 @@ export class KernelExecutor {
305284
case 'error':
306285
{
307286
const { ename, evalue, traceback } = content as KernelMessage.IReplyErrorContent;
287+
if (this._onCodeExecutionError) {
288+
this._executed.promise.catch(
289+
(err) => this._onCodeExecutionError && this._onCodeExecutionError(err)
290+
)
291+
}
308292
this._executed.reject(`${ename}: ${evalue}\n${(traceback ?? []).join('\n')}`);
309293
}
310294
break;

packages/react/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const ENTRY =
3939
// './src/examples/KernelExecutor';
4040
// './src/examples/Lumino';
4141
// './src/examples/Matplotlib';
42-
'./src/examples/Notebook';
42+
'./src/examples/Notebook';
4343
// './src/examples/NotebookUrl';
4444
// './src/examples/NotebookColorMode';
4545
// './src/examples/NotebookKernelChange';
@@ -54,6 +54,7 @@ const ENTRY =
5454
// './src/examples/NotebookThemeColorMode';
5555
// './src/examples/ObservableHQ';
5656
// './src/examples/Output';
57+
// './src/examples/OutputWithErrorHandle';
5758
// './src/examples/Outputs';
5859
// './src/examples/Plotly';
5960
// './src/examples/RunningSessions';

0 commit comments

Comments
 (0)