consistent handling of error messages, no double reporting

This commit is contained in:
Michael Bradley, Jr 2018-08-18 18:16:03 -05:00
parent ba98f76a10
commit 792a8f57eb

View File

@ -6,6 +6,16 @@ const writeFile = require('util').promisify(require('fs').writeFile);
let webpackProcess; let webpackProcess;
function errorMessage(e) {
if (typeof e === 'string') {
return e;
} else if (e && e.message) {
return e.message;
} else {
return e;
}
}
class WebpackProcess extends ProcessWrapper { class WebpackProcess extends ProcessWrapper {
constructor(options) { constructor(options) {
super(options); super(options);
@ -16,9 +26,7 @@ class WebpackProcess extends ProcessWrapper {
try { try {
await this.webpackRun(assets, importsList, callback); await this.webpackRun(assets, importsList, callback);
} catch (e) { } catch (e) {
console.error(e.message); callback(errorMessage(e));
callback(e);
// ?? should return e or e.message
} }
} }
@ -33,9 +41,7 @@ class WebpackProcess extends ProcessWrapper {
JSON.stringify(assets) JSON.stringify(assets)
); );
} catch (e) { } catch (e) {
console.error(e.message); return callback(errorMessage(e));
return callback(e);
// ?? should return e or e.message
} }
const dappConfigPath = fs.dappPath('webpack.config.js'); const dappConfigPath = fs.dappPath('webpack.config.js');
@ -60,15 +66,10 @@ class WebpackProcess extends ProcessWrapper {
} else if (Array.isArray(config)) { } else if (Array.isArray(config)) {
config = config.filter(cfg => cfg.name === this.webpackConfigName); config = config.filter(cfg => cfg.name === this.webpackConfigName);
if (!config.length) { if (!config.length) {
const errMsg = `no webpack config has the name '${this.webpackConfigName}'`; return callback(`no webpack config has the name '${this.webpackConfigName}'`);
console.error(errMsg);
return callback(errMsg);
// ?? should the message be wrapped in new Error()
} }
if (config.length > 1) { if (config.length > 1) {
console.warn( console.warn(`detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one`);
`detected ${config.length} webpack configs having the name '${this.webpackConfigName}', using the first one`
);
} }
config = config[0]; config = config[0];
} else { } else {
@ -76,22 +77,16 @@ class WebpackProcess extends ProcessWrapper {
} }
} catch (e) { } catch (e) {
console.error(`error while loading webpack config ${configPath}`); console.error(`error while loading webpack config ${configPath}`);
console.error(e.message); callback(errorMessage(e));
callback(e);
// ?? should return e or e.message
} }
if (typeof config !== 'object' || config === null) { if (typeof config !== 'object' || config === null) {
const errMsg = 'bad webpack config, the resolved config was null or not an object'; return callback('bad webpack config, the resolved config was null or not an object');
console.error(errMsg);
return callback(errMsg);
// ?? should the message be wrapped in new Error()
} }
webpack(config).run(async (err, stats) => { webpack(config).run(async (err, stats) => {
if (err) { if (err) {
console.error(err); return callback(errorMessage(err));
return callback(err);
} }
try { try {
if (config.stats && config.stats !== 'none') { if (config.stats && config.stats !== 'none') {
@ -107,12 +102,10 @@ class WebpackProcess extends ProcessWrapper {
); );
} }
} catch (e) { } catch (e) {
console.error(e.message); return callback(errorMessage(e));
return callback(err);
} }
if (config.stats && stats.hasErrors()) { if (config.stats && stats.hasErrors()) {
const errors = stats.toJson(config.stats).errors.join('\n'); const errors = stats.toJson(config.stats).errors.join('\n');
console.error(errors);
return callback(errors); return callback(errors);
} }
callback(); callback();