diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-06-20 07:13:26 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-06-27 20:22:08 +0800 |
commit | 94454927f697840a25c1ae73ebbcf9a5324b9060 (patch) | |
tree | 14d7579730ebbaf77d5f9b6945bbf408457b76e4 /lib/internal/errors.js | |
parent | ed8fc7e11d688cbcdf33d0d149830064758bdcd2 (diff) | |
download | android-node-v8-94454927f697840a25c1ae73ebbcf9a5324b9060.tar.gz android-node-v8-94454927f697840a25c1ae73ebbcf9a5324b9060.tar.bz2 android-node-v8-94454927f697840a25c1ae73ebbcf9a5324b9060.zip |
process: split routines used to enhance fatal exception stack traces
Previously the enhancement were done right after emitting
`'uncaughtException'`, which meant by the time we knew the
exception was fatal in C++, the error.stack had already been
patched.
This patch moves those routines to be called later during the
fatal exception handling, and split them into two stages:
before and after the inspector is notified by the invocation of
`V8Inspector::exceptionThrown`. We now expand the stack to include
additional informations about unhandled 'error' events before
the inspector is notified, but delay the highlighting of the
frames until after the inspector is notified, so that the
ANSI escape sequences won't show up in the inspector console.
PR-URL: https://github.com/nodejs/node/pull/28308
Fixes: https://github.com/nodejs/node/issues/28287
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'lib/internal/errors.js')
-rw-r--r-- | lib/internal/errors.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/internal/errors.js b/lib/internal/errors.js index a45d71e8f6..ff1449f519 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -622,6 +622,45 @@ function addNumericalSeparator(val) { return `${val.slice(0, i)}${res}`; } +// Used to enhance the stack that will be picked up by the inspector +const kEnhanceStackBeforeInspector = Symbol('kEnhanceStackBeforeInspector'); + +// These are supposed to be called only on fatal exceptions before +// the process exits. +const fatalExceptionStackEnhancers = { + beforeInspector(error) { + if (typeof error[kEnhanceStackBeforeInspector] !== 'function') { + return error.stack; + } + + try { + // Set the error.stack here so it gets picked up by the + // inspector. + error.stack = error[kEnhanceStackBeforeInspector](); + } catch { + // We are just enhancing the error. If it fails, ignore it. + } + return error.stack; + }, + afterInspector(error) { + const originalStack = error.stack; + const { + inspect, + inspectDefaultOptions: { + colors: defaultColors + } + } = lazyInternalUtilInspect(); + const colors = internalBinding('util').guessHandleType(2) === 'TTY' && + require('internal/tty').hasColors() || + defaultColors; + try { + return inspect(error, { colors }); + } catch { + return originalStack; + } + } +}; + module.exports = { addCodeToName, // Exported for NghttpError codes, @@ -638,6 +677,8 @@ module.exports = { // This is exported only to facilitate testing. E, prepareStackTrace, + kEnhanceStackBeforeInspector, + fatalExceptionStackEnhancers }; // To declare an error message, use the E(sym, val, def) function above. The sym |