diff --git a/lib/repl.js b/lib/repl.js index d4b95bf3a67f45..40c3a68714cd9f 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -257,12 +257,13 @@ function REPLServer(prompt, } } catch (e) { err = e; - if (err.message === 'Script execution interrupted.') { + + if (err && err.message === 'Script execution interrupted.') { // The stack trace for this case is not very useful anyway. Object.defineProperty(err, 'stack', { value: '' }); } - if (err && process.domain) { + if (process.domain) { debug('not recoverable, send to domain'); process.domain.emit('error', err); process.domain.exit(); diff --git a/test/parallel/test-repl-throw-null-or-undefined.js b/test/parallel/test-repl-throw-null-or-undefined.js new file mode 100644 index 00000000000000..fd2fd202b5bcb6 --- /dev/null +++ b/test/parallel/test-repl-throw-null-or-undefined.js @@ -0,0 +1,18 @@ +'use strict'; +require('../common'); + +// This test ensures that the repl does not +// crash or emit error when throwing `null|undefined` +// ie `throw null` or `throw undefined` + +const assert = require('assert'); +const repl = require('repl'); + +const r = repl.start(); + +assert.doesNotThrow(() => { + r.write('throw null\n'); + r.write('throw undefined\n'); +}, TypeError, 'repl crashes/throw error on `throw null|undefined`'); + +r.write('.exit\n');