Skip to content

Fix error handling in compiler watch() and run() #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/Shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function Shared(context) {
if(typeof options.reporter !== "function") options.reporter = share.defaultReporter;
if(typeof options.log !== "function") options.log = console.log.bind(console);
if(typeof options.warn !== "function") options.warn = console.warn.bind(console);
if(typeof options.error !== "function") options.error = console.error.bind(console);
if(typeof options.watchDelay !== "undefined") {
// TODO remove this in next major version
options.warn("options.watchDelay is deprecated: Use 'options.watchOptions.aggregateTimeout' instead");
Expand Down Expand Up @@ -149,9 +150,7 @@ module.exports = function Shared(context) {
var compiler = context.compiler;
// start watching
if(!options.lazy) {
var watching = compiler.watch(options.watchOptions, function(err) {
if(err) throw err;
});
var watching = compiler.watch(options.watchOptions, share.handleCompilerCallback);
context.watching = watching;
} else {
context.state = true;
Expand All @@ -160,13 +159,17 @@ module.exports = function Shared(context) {
rebuild: function rebuild() {
if(context.state) {
context.state = false;
context.compiler.run(function(err) {
if(err) throw err;
});
context.compiler.run(share.handleCompilerCallback);
} else {
context.forceRebuild = true;
}
},
handleCompilerCallback: function(err) {
if(err) {
context.options.error(err.stack || err);
if(err.details) context.options.error(err.details);
}
},
handleRequest: function(filename, processRequest, req) {
// in lazy mode, rebuild on bundle request
if(context.options.lazy && (!context.options.filename || context.options.filename.test(filename)))
Expand Down
40 changes: 40 additions & 0 deletions test/CompilerCallbacks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var should = require("should");
var middleware = require("../middleware");
require("mocha-sinon");

describe("CompilerCallbacks", function() {
var plugins = {};
var compiler = {
watch: function() {},
plugin: function(name, callback) {
plugins[name] = callback;
}
};
beforeEach(function() {
plugins = {};
});

it("watch error should be reported to console", function(done) {
var error = new Error("Oh noes!");
this.sinon.stub(compiler, "watch", function(opts, callback) {
callback(error);
});
this.sinon.stub(console, "error");
middleware(compiler);
should.strictEqual(console.error.callCount, 1);
should.strictEqual(console.error.calledWith(error.stack), true);
done();
});

it("options.error should be used on watch error", function(done) {
this.sinon.stub(compiler, "watch", function(opts, callback) {
callback(new Error("Oh noes!"));
});
middleware(compiler, {
error: function(err) {
err.should.startWith("Error: Oh noes!");
done();
}
});
});
});
14 changes: 9 additions & 5 deletions test/Lazy.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var should = require("should");
var middleware = require("../middleware");
require("mocha-sinon");

var doneStats = {
hasErrors: function() {
Expand Down Expand Up @@ -30,6 +31,7 @@ describe("Lazy mode", function() {
describe("builds", function() {
var req = { method: "GET", url: "/bundle.js" };
beforeEach(function() {
this.sinon.stub(console, "error");
instance = middleware(compiler, { lazy: true, quiet: true });
});
it("should trigger build", function(done) {
Expand All @@ -54,11 +56,13 @@ describe("Lazy mode", function() {
});
});

it("should pass through compiler error", function() {
compiler.run.callsArgWith(0, new Error("MyCompilerError"));
should.throws(function() {
instance(req, res, next);
}, "MyCompilerError");
it("should pass through compiler error", function(done) {
var error = new Error("MyCompilerError");
compiler.run.callsArgWith(0, error);
instance(req, res, next);
should.strictEqual(console.error.callCount, 1);
should.strictEqual(console.error.calledWith(error.stack), true);
done();
});
});

Expand Down