@@ -4,6 +4,11 @@ import { spawn, ChildProcess } from 'child_process';
44import { dirname } from 'path' ;
55import getPort = require( 'get-port' ) ;
66
7+ interface REvalOutput {
8+ type : "output" | "error" ;
9+ result : string ;
10+ }
11+
712class RKernel {
813 private kernelScript : string ;
914 private cwd : string ;
@@ -52,7 +57,7 @@ class RKernel {
5257 await this . start ( ) ;
5358 }
5459
55- public async eval ( cell : vscode . NotebookCell ) : Promise < string > {
60+ public async eval ( cell : vscode . NotebookCell ) : Promise < REvalOutput > {
5661 if ( this . process ) {
5762 const client = net . createConnection ( { port : this . port } , ( ) => {
5863 console . log ( 'connected to server!' ) ;
@@ -73,13 +78,17 @@ class RKernel {
7378 const response = data . toString ( ) ;
7479 console . log ( response ) ;
7580 client . end ( ) ;
76- const output = JSON . parse ( response ) ;
77- const result : string [ ] = output . result ;
78- resolve ( result . join ( '\n' ) ) ;
81+ const output : REvalOutput = JSON . parse ( response ) ;
82+ resolve ( output ) ;
7983 } ) ;
8084
8185 client . on ( 'error' , ( err ) => {
82- reject ( err . message ) ;
86+ reject ( {
87+ type : "error" ,
88+ result : [
89+ err . message
90+ ] ,
91+ } ) ;
8392 } ) ;
8493 } ) ;
8594 }
@@ -103,7 +112,7 @@ class RNotebook implements vscode.Disposable {
103112 this . kernel . restart ( ) ;
104113 }
105114
106- public async eval ( cell : vscode . NotebookCell ) : Promise < string > {
115+ public async eval ( cell : vscode . NotebookCell ) : Promise < REvalOutput > {
107116 await this . kernel . start ( ) ;
108117 return this . kernel . eval ( cell ) ;
109118 }
@@ -285,9 +294,12 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
285294 cell . metadata . runStartTime = start ;
286295 cell . metadata . executionOrder = ++ this . runIndex ;
287296 const output = await notebook . eval ( cell ) ;
297+ if ( output . type === "error" ) {
298+ throw new Error ( output . result ) ;
299+ }
288300 cell . outputs = [ {
289301 outputKind : vscode . CellOutputKind . Text ,
290- text : output ,
302+ text : output . result ,
291303 } ] ;
292304 cell . metadata . runState = vscode . NotebookCellRunState . Success ;
293305 cell . metadata . lastRunDuration = + new Date ( ) - start ;
@@ -298,6 +310,8 @@ export class RNotebookProvider implements vscode.NotebookContentProvider, vscode
298310 ename : '' ,
299311 traceback : [ ] ,
300312 } ] ;
313+ cell . metadata . runState = vscode . NotebookCellRunState . Error ;
314+ cell . metadata . lastRunDuration = undefined ;
301315 }
302316 }
303317 }
0 commit comments