summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2015-12-15 03:55:35 -0500
committerBrian White <mscdex@mscdex.net>2015-12-19 14:15:38 -0500
commit18490d3d5af3131751791e501302bc57865337b3 (patch)
tree78cb704b8126394b9b632484ca7bde17a06914e3 /lib
parent4b0b991bf5bbb41e204a7b98f62a8afd00faf855 (diff)
downloadnode-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.js10
-rw-r--r--lib/module.js14
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.