@@ -53,10 +53,20 @@ export class JavaScriptRuntimeEvaluator {
5353 code : string ,
5454 executionCount : number
5555 ) : Promise < KernelMessage . IExecuteReplyMsg [ 'content' ] > {
56+ // Parse-time errors are syntax errors, so show only `Name: message`.
57+ let asyncFunction : ( ) => Promise < any > ;
58+ let withReturn : boolean ;
5659 try {
57- const { asyncFunction, withReturn } =
58- this . _executor . makeAsyncFromCode ( code ) ;
60+ const parsed = this . _executor . makeAsyncFromCode ( code ) ;
61+ asyncFunction = parsed . asyncFunction ;
62+ withReturn = parsed . withReturn ;
63+ } catch ( error ) {
64+ const normalized = normalizeError ( error ) ;
65+ return this . _emitError ( executionCount , normalized , false ) ;
66+ }
5967
68+ // Runtime errors may include useful eval frames from user code.
69+ try {
6070 const resultPromise = this . _evalFunc ( asyncFunction ) ;
6171
6272 if ( withReturn ) {
@@ -84,24 +94,7 @@ export class JavaScriptRuntimeEvaluator {
8494 } ;
8595 } catch ( error ) {
8696 const normalized = normalizeError ( error ) ;
87- const cleanedStack = this . _executor . cleanStackTrace ( normalized ) ;
88-
89- const content : KernelMessage . IReplyErrorContent = {
90- status : 'error' ,
91- ename : normalized . name || 'Error' ,
92- evalue : normalized . message || '' ,
93- traceback : [ cleanedStack ]
94- } ;
95-
96- this . _onOutput ( {
97- type : 'execute_error' ,
98- bundle : content
99- } ) ;
100-
101- return {
102- ...content ,
103- execution_count : executionCount
104- } ;
97+ return this . _emitError ( executionCount , normalized , true ) ;
10598 }
10699 }
107100
@@ -148,6 +141,36 @@ export class JavaScriptRuntimeEvaluator {
148141 return asyncFunc . call ( this . _globalScope ) ;
149142 }
150143
144+ /**
145+ * Build and emit an execute error reply.
146+ */
147+ private _emitError (
148+ executionCount : number ,
149+ error : Error ,
150+ includeStack : boolean
151+ ) : KernelMessage . IExecuteReplyMsg [ 'content' ] {
152+ const traceback = includeStack
153+ ? this . _executor . cleanStackTrace ( error )
154+ : `${ error . name } : ${ error . message } ` ;
155+
156+ const content : KernelMessage . IReplyErrorContent = {
157+ status : 'error' ,
158+ ename : error . name || 'Error' ,
159+ evalue : error . message || '' ,
160+ traceback : [ traceback ]
161+ } ;
162+
163+ this . _onOutput ( {
164+ type : 'execute_error' ,
165+ bundle : content
166+ } ) ;
167+
168+ return {
169+ ...content ,
170+ execution_count : executionCount
171+ } ;
172+ }
173+
151174 /**
152175 * Patch console methods in runtime scope to emit Jupyter stream messages.
153176 */
0 commit comments