From bfa36136dac787793bc5106da57cccbb8db1ce14 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 18 Dec 2009 23:58:04 +0100 Subject: [PATCH] require() should throw error if module does. Reported by Kris Zyp http://groups.google.com/group/nodejs/browse_thread/thread/1feab0309bd5402b --- src/node.js | 7 ++++++- test/mjsunit/fixtures/throws_error.js | 1 + test/mjsunit/test-module-loading.js | 10 ++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/mjsunit/fixtures/throws_error.js diff --git a/src/node.js b/src/node.js index ef276ec46d..76dd8a6a22 100644 --- a/src/node.js +++ b/src/node.js @@ -892,7 +892,12 @@ Module.prototype.loadScript = function (filename, loadPromise) { + "\n}; __wrap__;"; var compiledWrapper = process.compile(wrapper, filename); - compiledWrapper.apply(self.exports, [self.exports, require, self, filename]); + try { + compiledWrapper.apply(self.exports, [self.exports, require, self, filename]); + } catch (e) { + loadPromise.emitError(e); + return; + } self.waitChildrenLoad(function () { self.loaded = true; diff --git a/test/mjsunit/fixtures/throws_error.js b/test/mjsunit/fixtures/throws_error.js new file mode 100644 index 0000000000..728ece555f --- /dev/null +++ b/test/mjsunit/fixtures/throws_error.js @@ -0,0 +1 @@ +throw new Error("blah"); diff --git a/test/mjsunit/test-module-loading.js b/test/mjsunit/test-module-loading.js index bba920b9bb..c6aea25384 100644 --- a/test/mjsunit/test-module-loading.js +++ b/test/mjsunit/test-module-loading.js @@ -33,6 +33,14 @@ assert.equal("D", d3.D()); assert.equal(true, d4.D instanceof Function); assert.equal("D", d4.D()); +var errorThrown = false; +try { + require("./fixtures/throws_error"); +} catch (e) { + errorThrown = true; + assert.equal("blah", e.message); +} + process.addListener("exit", function () { assert.equal(true, a.A instanceof Function); assert.equal("A done", a.A()); @@ -49,5 +57,7 @@ process.addListener("exit", function () { assert.equal(true, d2.D instanceof Function); assert.equal("D done", d2.D()); + assert.equal(true, errorThrown); + puts("exit"); });