diff options
author | Brian White <mscdex@mscdex.net> | 2015-12-15 03:55:35 -0500 |
---|---|---|
committer | Brian White <mscdex@mscdex.net> | 2015-12-19 14:15:38 -0500 |
commit | 18490d3d5af3131751791e501302bc57865337b3 (patch) | |
tree | 78cb704b8126394b9b632484ca7bde17a06914e3 /lib | |
parent | 4b0b991bf5bbb41e204a7b98f62a8afd00faf855 (diff) | |
download | node-new-18490d3d5af3131751791e501302bc57865337b3.tar.gz |
module: always decorate thrown errors
This provides more information when encountering a syntax or similar
error when executing a file with require().
Fixes: https://github.com/nodejs/node/issues/4286
PR-URL: https://github.com/nodejs/node/pull/4287
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/util.js | 10 | ||||
-rw-r--r-- | lib/module.js | 14 |
2 files changed, 19 insertions, 5 deletions
diff --git a/lib/internal/util.js b/lib/internal/util.js index 78254ceb82..21aafff218 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -4,6 +4,7 @@ const binding = process.binding('util'); const prefix = `(${process.release.name}:${process.pid}) `; exports.getHiddenValue = binding.getHiddenValue; +exports.setHiddenValue = binding.setHiddenValue; // All the internal deprecations have to use this function only, as this will // prepend the prefix to the actual message. @@ -76,13 +77,16 @@ exports._deprecate = function(fn, msg) { }; exports.decorateErrorStack = function decorateErrorStack(err) { - if (!(exports.isError(err) && err.stack)) + if (!(exports.isError(err) && err.stack) || + exports.getHiddenValue(err, 'node:decorated') === true) return; - const arrow = exports.getHiddenValue(err, 'arrowMessage'); + const arrow = exports.getHiddenValue(err, 'node:arrowMessage'); - if (arrow) + if (arrow) { err.stack = arrow + err.stack; + exports.setHiddenValue(err, 'node:decorated', true); + } }; exports.isError = function isError(e) { diff --git a/lib/module.js b/lib/module.js index 8feba15b0f..82b1971e8b 100644 --- a/lib/module.js +++ b/lib/module.js @@ -23,6 +23,16 @@ function hasOwnProperty(obj, prop) { } +function tryWrapper(wrapper, opts) { + try { + return runInThisContext(wrapper, opts); + } catch (e) { + internalUtil.decorateErrorStack(e); + throw e; + } +} + + function Module(id, parent) { this.id = id; this.exports = {}; @@ -371,8 +381,8 @@ Module.prototype._compile = function(content, filename) { // create wrapper function var wrapper = Module.wrap(content); - var compiledWrapper = runInThisContext(wrapper, - { filename: filename, lineOffset: 0 }); + var compiledWrapper = tryWrapper(wrapper, + { filename: filename, lineOffset: 0 }); if (global.v8debug) { if (!resolvedArgv) { // we enter the repl if we're not given a filename argument. |