Skip to content

Commit bf9d8e9

Browse files
felixgeisaacs
authored andcommitted
Fix exception output for module load exceptions
So instead of: node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ You will now see: path/to/foo.js:1 throw new Error('bar'); ^ This is a sub-set of isaacs patch here: nodejs/node-v0.x-archive#3235 The difference is that this patch purely adresses the exception output, but does not try to make any behavior changes / improvements.
1 parent 8140333 commit bf9d8e9

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

lib/module.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,16 @@ Module._load = function(request, parent, isMain) {
304304
}
305305

306306
Module._cache[filename] = module;
307+
308+
var hadException = true;
309+
307310
try {
308311
module.load(filename);
309-
} catch (err) {
310-
delete Module._cache[filename];
311-
throw err;
312+
hadException = false;
313+
} finally {
314+
if (hadException) {
315+
delete Module._cache[filename];
316+
}
312317
}
313318

314319
return module.exports;

test/message/stack_overflow.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
before
22

3-
module.js:311
4-
throw err;
5-
^
3+
*test*message*stack_overflow.js:31
4+
function stackOverflow() {
5+
^
66
RangeError: Maximum call stack size exceeded
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
before
22

3-
module.js:311
4-
throw err;
5-
^
3+
*test*message*throw_custom_error.js:31
4+
throw { name: 'MyCustomError', message: 'This is a custom message' };
5+
^
66
MyCustomError: This is a custom message

test/message/throw_non_error.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
before
22

3-
module.js:311
4-
throw err;
5-
^
3+
*/test/message/throw_non_error.js:31
4+
throw { foo: 'bar' };
5+
^
66
[object Object]

test/message/undefined_reference_in_new_context.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
before
22

3-
module.js:311
4-
throw err;
5-
^
3+
*test*message*undefined_reference_in_new_context.js:34
4+
script.runInNewContext();
5+
^
66
ReferenceError: foo is not defined
77
at evalmachine.<anonymous>:*
88
at Object.<anonymous> (*test*message*undefined_reference_in_new_context.js:*)

0 commit comments

Comments
 (0)