Skip to content

Commit 9ba9d6f

Browse files
fix: output stacktrace on errors (#1949)
1 parent 564279e commit 9ba9d6f

File tree

11 files changed

+38
-22
lines changed

11 files changed

+38
-22
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jobs:
9191

9292
- name: Run tests for webpack version ${{ matrix.webpack-version }}
9393
run: |
94+
yarn run lerna bootstrap
9495
yarn prepsuite
9596
yarn test:ci
9697
env:

packages/generators/src/addon-generator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ const addonGenerator = (
5454
const pathToProjectDir: string = this.destinationPath(this.props.name);
5555
try {
5656
mkdirp.sync(pathToProjectDir);
57-
} catch (err) {
58-
logger.error('Failed to create directory', err);
57+
} catch (error) {
58+
logger.error('Failed to create directory');
59+
logger.error(error);
5960
}
6061
this.destinationRoot(pathToProjectDir);
6162
}

packages/webpack-cli/lib/groups/ConfigGroup.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const { resolve, extname } = require('path');
33
const webpackMerge = require('webpack-merge');
44
const { extensions, jsVariants } = require('interpret');
55
const rechoir = require('rechoir');
6-
const ConfigError = require('../utils/errors/ConfigError');
76
const logger = require('../utils/logger');
87

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

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

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

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

packages/webpack-cli/lib/utils/errors/ConfigError.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/webpack-cli/lib/utils/logger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
const util = require('util');
12
const { red, cyan, yellow, green } = require('colorette');
23

34
module.exports = {
4-
error: (val) => console.error(`[webpack-cli] ${red(val)}`),
5+
error: (val) => console.error(`[webpack-cli] ${red(util.format(val))}`),
56
warn: (val) => console.warn(`[webpack-cli] ${yellow(val)}`),
67
info: (val) => console.info(`[webpack-cli] ${cyan(val)}`),
78
success: (val) => console.log(`[webpack-cli] ${green(val)}`),

test/config/absent/config-absent.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Config:', () => {
1111
expect(stdout).toBeFalsy();
1212
const configPath = resolve(__dirname, 'webpack.config.js');
1313
// Should contain the correct error message
14-
expect(stderr).toContain(`ConfigError: The specified config file doesn't exist in ${configPath}`);
14+
expect(stderr).toContain(`The specified config file doesn't exist in ${configPath}`);
1515
// Should not bundle
1616
expect(existsSync(resolve(__dirname, './binary/a.bundle.js'))).toBeFalsy();
1717
});

test/error/error.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const { run } = require('../utils/test-utils');
4+
5+
describe('error', () => {
6+
it('should log error with stacktrace', async () => {
7+
const { stderr, stdout, exitCode } = await run(__dirname);
8+
9+
expect(stderr).toContain('Error: test');
10+
expect(stderr).toMatch(/at .+ (.+)/);
11+
expect(stdout).toBeFalsy();
12+
expect(exitCode).toBe(2);
13+
});
14+
});

test/error/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'foo';

test/error/webpack.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
plugins: [
3+
{
4+
apply() {
5+
throw new Error('test');
6+
},
7+
},
8+
],
9+
};

test/merge/config-absent/merge-config-absent.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('merge flag configuration', () => {
1313
// Since the process will exit, nothing on stdout
1414
expect(stdout).toBeFalsy();
1515
// Confirm that the user is notified
16-
expect(stderr).toContain(`MergeError: Atleast two configurations are required for merge.`);
16+
expect(stderr).toContain('At least two configurations are required for merge.');
1717
// Default config would be used
1818
expect(fs.existsSync(join(__dirname, './dist/merged.js'))).toBeFalsy();
1919
// Since the process will exit so no compilation will be done

0 commit comments

Comments
 (0)