Skip to content
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

fix: output stacktrace on errors #1949

Merged
merged 5 commits into from
Oct 16, 2020
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
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'foo';
9 changes: 9 additions & 0 deletions test/error/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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