Skip to content

Commit bfa3613

Browse files
committed
require() should throw error if module does.
Reported by Kris Zyp http://groups.google.com/group/nodejs/browse_thread/thread/1feab0309bd5402b
1 parent 4526308 commit bfa3613

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/node.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,12 @@ Module.prototype.loadScript = function (filename, loadPromise) {
892892
+ "\n}; __wrap__;";
893893
var compiledWrapper = process.compile(wrapper, filename);
894894

895-
compiledWrapper.apply(self.exports, [self.exports, require, self, filename]);
895+
try {
896+
compiledWrapper.apply(self.exports, [self.exports, require, self, filename]);
897+
} catch (e) {
898+
loadPromise.emitError(e);
899+
return;
900+
}
896901

897902
self.waitChildrenLoad(function () {
898903
self.loaded = true;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error("blah");

test/mjsunit/test-module-loading.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ assert.equal("D", d3.D());
3333
assert.equal(true, d4.D instanceof Function);
3434
assert.equal("D", d4.D());
3535

36+
var errorThrown = false;
37+
try {
38+
require("./fixtures/throws_error");
39+
} catch (e) {
40+
errorThrown = true;
41+
assert.equal("blah", e.message);
42+
}
43+
3644
process.addListener("exit", function () {
3745
assert.equal(true, a.A instanceof Function);
3846
assert.equal("A done", a.A());
@@ -49,5 +57,7 @@ process.addListener("exit", function () {
4957
assert.equal(true, d2.D instanceof Function);
5058
assert.equal("D done", d2.D());
5159

60+
assert.equal(true, errorThrown);
61+
5262
puts("exit");
5363
});

0 commit comments

Comments
 (0)