-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repl: handle unexpected error objects #12400
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,17 +282,25 @@ function REPLServer(prompt, | |
self._domain.on('error', function debugDomainError(e) { | ||
debug('domain error'); | ||
const top = replMap.get(self); | ||
var out; | ||
internalUtil.decorateErrorStack(e); | ||
if (e instanceof SyntaxError && e.stack) { | ||
|
||
if (!((e instanceof Error) && typeof e.stack === 'string' && | ||
e.stack.length > 0)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how robust you want this code to be, but the function FakeError() {}
FakeError.prototype = Object.create(Error.prototype, {
stack: { get() { throw new Error(); } }
});
var e = new FakeError(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how others feel, but I'm OK with it, at least in the context of this PR. It seems like any Node application can be crashed with a well crafted getter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing that I would (personally) prefer is either using V8’s There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noting that whatever is done here would need to factor in compatibility with |
||
out = util.inspect(e); | ||
} else if (e instanceof SyntaxError) { | ||
// remove repl:line-number and stack trace | ||
e.stack = e.stack | ||
.replace(/^repl:\d+\r?\n/, '') | ||
.replace(/^\s+at\s.*\n?/gm, ''); | ||
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) { | ||
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/, | ||
(_, pre, line) => pre + (line - 1)); | ||
out = e.stack | ||
.replace(/^repl:\d+\r?\n/, '') | ||
.replace(/^\s+at\s.*\n?/gm, ''); | ||
} else if (self.replMode === exports.REPL_MODE_STRICT) { | ||
out = e.stack.replace(/(\s+at\s+repl:)(\d+)/, | ||
(_, pre, line) => pre + (line - 1)); | ||
} else { | ||
out = e.stack; | ||
} | ||
top.outputStream.write((e.stack || e) + '\n'); | ||
|
||
top.outputStream.write(out + '\n'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will fail if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about if |
||
top.bufferedCommand = ''; | ||
top.lines.level = []; | ||
top.displayPrompt(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,27 @@ function error_test() { | |
// Uncaught error throws and prints out | ||
{ client: client_unix, send: 'throw new Error(\'test error\');', | ||
expect: /^Error: test error/ }, | ||
// Throws primitive and prints out | ||
{ client: client_unix, send: 'throw null;', | ||
expect: 'null' }, | ||
// Throws Object without toString() method and prints out | ||
{ client: client_unix, send: 'throw Object.create(null);', | ||
expect: '{}' }, | ||
// Throws Object with bad toString() method and prints out | ||
{ | ||
client: client_unix, | ||
send: 'var e = { toString() { throw new Error(\'test\'); } }; throw e;', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even smarter! 👍 |
||
expect: /{ toString: \[Function: toString\] }/ | ||
}, | ||
// Throws Symbol and prints out | ||
{ client: client_unix, send: 'throw Symbol(\'test\');', | ||
expect: /^Symbol\(test\)/ }, | ||
// Throws Error without stack and prints out | ||
{ | ||
client: client_unix, | ||
send: 'var e = new Error(\'test error\'); delete e.stack; throw e;', | ||
expect: /^\[Error: test error\]/ | ||
}, | ||
// Common syntax error is treated as multiline command | ||
{ client: client_unix, send: 'function test_func() {', | ||
expect: prompt_multiline }, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We haven't moved over to
let
completely yet.