Skip to content

Commit 34821f6

Browse files
Benjamin GruenbaumFishrock123
authored andcommitted
repl: don't terminate on null thrown
Previous behavior was to assume an error is a proper error in the repl module. A check was added to not terminate the process on thrown repl errors that are `null` or `undefined`. PR-URL: #14306Fixes: #12373 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]
1 parent 53ad91c commit 34821f6

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

‎lib/repl.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,23 @@ function REPLServer(prompt,
282282
self._domain.on('error',functiondebugDomainError(e){
283283
debug('domain error');
284284
consttop=replMap.get(self);
285+
285286
internalUtil.decorateErrorStack(e);
287+
constisError=internalUtil.isError(e);
286288
if(einstanceofSyntaxError&&e.stack){
287289
// remove repl:line-number and stack trace
288290
e.stack=e.stack
289291
.replace(/^repl:\d+\r?\n/,'')
290292
.replace(/^\s+at\s.*\n?/gm,'');
291-
}elseif(e.stack&&self.replMode===exports.REPL_MODE_STRICT){
293+
}elseif(isError&&self.replMode===exports.REPL_MODE_STRICT){
292294
e.stack=e.stack.replace(/(\s+at\s+repl:)(\d+)/,
293295
(_,pre,line)=>pre+(line-1));
294296
}
295-
top.outputStream.write((e.stack||e)+'\n');
297+
if(isError&&e.stack){
298+
top.outputStream.write(`${e.stack}\n`);
299+
}else{
300+
top.outputStream.write(`Thrown: ${String(e)}\n`);
301+
}
296302
top.bufferedCommand='';
297303
top.lines.level=[];
298304
top.displayPrompt();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
require('../common');
3+
constrepl=require('repl');
4+
constassert=require('assert');
5+
constStream=require('stream');
6+
7+
constoutput=newStream();
8+
lettext='';
9+
output.write=output.pause=output.resume=function(buf){
10+
text+=buf.toString();
11+
};
12+
13+
constreplserver=repl.start({
14+
output: output,
15+
input: process.stdin
16+
});
17+
18+
replserver.emit('line','process.nextTick(() =>{throw null})');
19+
replserver.emit('line','.exit');
20+
21+
setTimeout(()=>{
22+
console.log(text);
23+
assert(text.includes('Thrown: null'));
24+
},0);

0 commit comments

Comments
(0)