Skip to content

Commit a88fdc7

Browse files
refactor(bin/webpack): use err.name for error handling
1 parent b3f3eda commit a88fdc7

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

bin/webpack.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,19 @@ yargs.parse(process.argv.slice(2), (err, argv, output) => {
333333
var compiler;
334334
try {
335335
compiler = webpack(options);
336-
} catch(e) {
337-
var WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
338-
if(e instanceof WebpackOptionsValidationError) {
336+
} catch(err) {
337+
if(err.name === "WebpackOptionsValidationError") {
339338
if(argv.color)
340-
console.error("\u001b[1m\u001b[31m" + e.message + "\u001b[39m\u001b[22m");
339+
console.error(
340+
`\u001b[1m\u001b[31m${err.message}\u001b[39m\u001b[22m`
341+
);
341342
else
342-
console.error(e.message);
343-
process.exit(1); // eslint-disable-line no-process-exit
343+
console.error(err.message);
344+
// eslint-disable-next-line no-process-exit
345+
process.exit(1);
344346
}
345-
throw e;
347+
348+
throw err;
346349
}
347350

348351
if(argv.progress) {

test/Validation.test.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"use strict";
33

44
require("should");
5+
56
const webpack = require("../lib/webpack");
6-
const WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
77

88
describe("Validation", () => {
99
const testCases = [{
@@ -207,30 +207,33 @@ describe("Validation", () => {
207207
foobar: true
208208
}
209209
},
210-
test(e) {
211-
e.message.should.startWith("Invalid configuration object.");
212-
e.message.split("\n").slice(1)[0].should.be.eql(
210+
test(err) {
211+
err.message.should.startWith("Invalid configuration object.");
212+
err.message.split("\n").slice(1)[0].should.be.eql(
213213
" - configuration.stats should be one of these:"
214214
);
215215
}
216216
}];
217+
217218
testCases.forEach((testCase) => {
218219
it("should fail validation for " + testCase.name, () => {
219220
try {
220221
webpack(testCase.config);
221-
} catch(e) {
222-
if(!(e instanceof WebpackOptionsValidationError))
223-
throw e;
222+
} catch(err) {
223+
if(err.name !== 'WebpackOptionsValidationError') throw err;
224224

225225
if(testCase.test) {
226-
testCase.test(e);
226+
testCase.test(err);
227+
227228
return;
228229
}
229230

230-
e.message.should.startWith("Invalid configuration object.");
231-
e.message.split("\n").slice(1).should.be.eql(testCase.message);
231+
err.message.should.startWith("Invalid configuration object.");
232+
err.message.split("\n").slice(1).should.be.eql(testCase.message);
233+
232234
return;
233235
}
236+
234237
throw new Error("Validation didn't fail");
235238
});
236239
});

0 commit comments

Comments
 (0)