Uh oh!
There was an error while loading. Please reload this page.
repl: handle unexpected error objects - #12400
Conversation
There was a problem hiding this comment.
It will fail if e is a Symbol, won't it?
There was a problem hiding this comment.
We haven't moved over to let completely yet.
There was a problem hiding this comment.
if there a chance e.stack != true || !('replace' in e.stack)?
Maybe a custom error:
function MySillyError(a, b, c) { console.log(a) }
MySillyError.prototype = SyntaxError
There was a problem hiding this comment.
I don't think we need to jump through hoops for extreme edge cases like this. As long as they don't crash.
There was a problem hiding this comment.
Cool, just tested, no crash just TypeError: Function.prototype.toString is not generic
There was a problem hiding this comment.
how about if delete e.toString?
There was a problem hiding this comment.
add this monstrosity:
send: 'function MySillyError() {}; MySillyError.prototype = SyntaxError; var e = new MySillyError(); throw e',expect: /^TypeError:Function.prototype.toStringisnotgeneric/There was a problem hiding this comment.
No crash, but INHO it's a good test
also @aqrln's
send: 'throw Symbol.for',expect: /^functionfor(){[nativecode]}/refack
commented
Apr 13, 2017
@cjihrig you're not a first time contributor 😁 kudos on the commitment... I added some horrible code to try and crash your cases... |
There was a problem hiding this comment.
Symbols and other bad inputs are taken care of here if I move to out = util.inspect(e);. However, it causes test-repl-sigint.js and test-repl-sigint-nested-eval.js to fail. @addaleax any idea why that would be?
There was a problem hiding this comment.
@addaleax I just pushed up the changes that include that and more tests, if you want to take a look.
There was a problem hiding this comment.
Anyway I would "de-morgan" it to:
(!(einstanceofError)||typeofe.stack!=='string')then add the original gourd🎃 guard
(typeofe==='undefined'||!(einstanceofError)||typeofe.stack!=='string')There was a problem hiding this comment.
Thanks @addaleax. The special snowflake that is the REPL. I added a length check for stack, so errors with empty stacks will print with util.inspect().
refack
commented
Apr 13, 2017
mo' tests less trouble?! |
refack
commented
Apr 13, 2017
You could add the extreme edge cases as tests send: 'function MySillyError() {}; MySillyError.prototype = SyntaxError; var e = new MySillyError(); throw e',expect: /^TypeError:Function.prototype.toStringisnotgeneric/also @aqrln's send: 'throw Symbol.for',expect: /^functionfor(){[nativecode]}/ |
benjamingr
left a comment
There was a problem hiding this comment.
Changes LGTM and improve current status quo.
This commit allows the repl's domain error handler to process unexpected error formats, such as primitives.
| // Throws Object with bad toString() method and prints out | ||
| { | ||
| client: client_unix, | ||
| send: 'var e = { toString() { throw new Error(\'test\'); } }; throw 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.
Not sure how robust you want this code to be, but the typeof e.stack will crash with
functionFakeError(){}FakeError.prototype=Object.create(Error.prototype,{stack: {get(){thrownewError();}}});vare=newFakeError();There was a problem hiding this comment.
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.
One thing that I would (personally) prefer is either using V8’s IsNativeError over instanceof Error or going full 🦆 here. Both would increase robustness in different ways :)
There was a problem hiding this comment.
Just noting that whatever is done here would need to factor in compatibility with internal/errors.js
Refs: nodejs#12400 PR-URL: nodejs#12546 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
addaleax
commented
May 26, 2017
@cjihrig Did you mean to close this? |
cjihrig
commented
Jun 6, 2017
Sorry @addaleax. Yes, I did. The move to |
TimothyGu
commented
Jun 17, 2017
Can you elaborate? The Node.js error classes all extend from the native error classes, and generally V8's methods recognize subclasses. |
cjihrig
commented
Jun 19, 2017
Hm, good question. I tested this before, but maybe I did something wrong. |
This commit allows the repl's domain error handler to process
unexpected error formats, such as primitives.
Fixes: #12373
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passesAffected core subsystem(s)
repl