Skip to content

Commit

Permalink
fix: output stacktrace on errors (#1949)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 16, 2020
1 parent 564279e commit 9ba9d6f
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Expand Up @@ -91,6 +91,7 @@ jobs:

- name: Run tests for webpack version ${{ matrix.webpack-version }}
run: |
yarn run lerna bootstrap
yarn prepsuite
yarn test:ci
env:
Expand Down
5 changes: 3 additions & 2 deletions packages/generators/src/addon-generator.ts
Expand Up @@ -54,8 +54,9 @@ const addonGenerator = (
const pathToProjectDir: string = this.destinationPath(this.props.name);
try {
mkdirp.sync(pathToProjectDir);
} catch (err) {
logger.error('Failed to create directory', err);
} catch (error) {
logger.error('Failed to create directory');
logger.error(error);
}
this.destinationRoot(pathToProjectDir);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -3,7 +3,6 @@ const { resolve, extname } = require('path');
const webpackMerge = require('webpack-merge');
const { extensions, jsVariants } = require('interpret');
const rechoir = require('rechoir');
const ConfigError = require('../utils/errors/ConfigError');
const logger = require('../utils/logger');

// Order defines the priority, in increasing order
Expand Down Expand Up @@ -92,7 +91,8 @@ const resolveConfigFiles = async (args) => {
const configFiles = getConfigInfoFromFileName(configPath);

if (!configFiles.length) {
throw new ConfigError(`The specified config file doesn't exist in ${configPath}`);
logger.error(`The specified config file doesn't exist in ${configPath}`);
process.exit(2);
}

const foundConfig = configFiles[0];
Expand Down Expand Up @@ -230,12 +230,12 @@ const resolveConfigMerging = async (args) => {
// either by passing multiple configs by flags or passing a
// single config exporting an array
if (!Array.isArray(configOptions)) {
throw new ConfigError('Atleast two configurations are required for merge.', 'MergeError');
logger.error('At least two configurations are required for merge.');
process.exit(2);
}

// We return a single config object which is passed to the compiler
const mergedOptions = configOptions.reduce((currentConfig, mergedConfig) => webpackMerge(currentConfig, mergedConfig), {});
opts['options'] = mergedOptions;
opts['options'] = configOptions.reduce((currentConfig, mergedConfig) => webpackMerge(currentConfig, mergedConfig), {});
}
};

Expand Down
11 changes: 0 additions & 11 deletions packages/webpack-cli/lib/utils/errors/ConfigError.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/webpack-cli/lib/utils/logger.js
@@ -1,7 +1,8 @@
const util = require('util');
const { red, cyan, yellow, green } = require('colorette');

module.exports = {
error: (val) => console.error(`[webpack-cli] ${red(val)}`),
error: (val) => console.error(`[webpack-cli] ${red(util.format(val))}`),
warn: (val) => console.warn(`[webpack-cli] ${yellow(val)}`),
info: (val) => console.info(`[webpack-cli] ${cyan(val)}`),
success: (val) => console.log(`[webpack-cli] ${green(val)}`),
Expand Down
2 changes: 1 addition & 1 deletion test/config/absent/config-absent.test.js
Expand Up @@ -11,7 +11,7 @@ describe('Config:', () => {
expect(stdout).toBeFalsy();
const configPath = resolve(__dirname, 'webpack.config.js');
// Should contain the correct error message
expect(stderr).toContain(`ConfigError: The specified config file doesn't exist in ${configPath}`);
expect(stderr).toContain(`The specified config file doesn't exist in ${configPath}`);
// Should not bundle
expect(existsSync(resolve(__dirname, './binary/a.bundle.js'))).toBeFalsy();
});
Expand Down
14 changes: 14 additions & 0 deletions test/error/error.test.js
@@ -0,0 +1,14 @@
'use strict';

const { run } = require('../utils/test-utils');

describe('error', () => {
it('should log error with stacktrace', async () => {
const { stderr, stdout, exitCode } = await run(__dirname);

expect(stderr).toContain('Error: test');
expect(stderr).toMatch(/at .+ (.+)/);
expect(stdout).toBeFalsy();
expect(exitCode).toBe(2);
});
});
1 change: 1 addition & 0 deletions test/error/src/index.js
@@ -0,0 +1 @@
module.exports = 'foo';
9 changes: 9 additions & 0 deletions test/error/webpack.config.js
@@ -0,0 +1,9 @@
module.exports = {
plugins: [
{
apply() {
throw new Error('test');
},
},
],
};
2 changes: 1 addition & 1 deletion test/merge/config-absent/merge-config-absent.test.js
Expand Up @@ -13,7 +13,7 @@ describe('merge flag configuration', () => {
// Since the process will exit, nothing on stdout
expect(stdout).toBeFalsy();
// Confirm that the user is notified
expect(stderr).toContain(`MergeError: Atleast two configurations are required for merge.`);
expect(stderr).toContain('At least two configurations are required for merge.');
// Default config would be used
expect(fs.existsSync(join(__dirname, './dist/merged.js'))).toBeFalsy();
// Since the process will exit so no compilation will be done
Expand Down
2 changes: 1 addition & 1 deletion test/merge/config/merge-config.test.js
Expand Up @@ -22,7 +22,7 @@ describe('merge flag configuration', () => {
});
it('fails when there are less than 2 configurations to merge', () => {
const { stdout, stderr, exitCode } = run(__dirname, ['--config', './1.js', '-m'], false);
expect(stderr).toContain(`MergeError: Atleast two configurations are required for merge.`);
expect(stderr).toContain('At least two configurations are required for merge.');
expect(stdout).toBeFalsy();
expect(exitCode).toBe(2);
});
Expand Down

0 comments on commit 9ba9d6f

Please sign in to comment.