Skip to content

Commit b671783

Browse files
committed
bug #520 Improve 'Install x to use y' and 'Unrecognized method' error messages. (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Improve 'Install x to use y' and 'Unrecognized method' error messages. This PR: - Removes the stacktrace from the "_Install x to use y_" error message by throwing a string instead of an `Error` (fixes #420) - Only keeps the second line of the stacktrace for the "_Unrecognized method_" error message (since this is most likely the one related to the `webpack.config.js` file) - Fixes the "_Did you mean xxxx_" advice that was broken since the `Encore` object became a `class` --- **Install x to use y** Before: ![2019-02-11_16-43-47](https://user-images.githubusercontent.com/850046/52579209-03450280-2e26-11e9-81a0-712ef6cec5fd.png) After: ![2019-02-11_16-42-13](https://user-images.githubusercontent.com/850046/52579241-0fc95b00-2e26-11e9-8bf4-9c98d628bff7.png) --- **Unrecognized method** Before: ![2019-02-11_16-44-43](https://user-images.githubusercontent.com/850046/52579263-1952c300-2e26-11e9-8e68-b489089ad383.png) After: ![2019-02-11_16-41-18](https://user-images.githubusercontent.com/850046/52579286-22dc2b00-2e26-11e9-8011-13807d33870b.png) Commits ------- 6160e36 Improve 'Install x to use y' and 'Unrecognized method' error messages.
2 parents 1f3be51 + 6160e36 commit b671783

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,13 @@ const EncoreProxy = new Proxy(new Encore(), {
12891289
// Find the property with the closest Levenshtein distance
12901290
let similarProperty;
12911291
let minDistance = Number.MAX_VALUE;
1292-
for (const apiProperty in target) {
1292+
1293+
for (const apiProperty of Object.getOwnPropertyNames(Encore.prototype)) {
1294+
// Ignore class constructor
1295+
if (apiProperty === 'constructor') {
1296+
continue;
1297+
}
1298+
12931299
const distance = levenshtein.get(apiProperty, prop);
12941300
if (distance <= minDistance) {
12951301
similarProperty = apiProperty;
@@ -1302,8 +1308,14 @@ const EncoreProxy = new Proxy(new Encore(), {
13021308
errorMessage += ` Did you mean ${chalk.green(`Encore.${similarProperty}`)}?`;
13031309
}
13041310

1311+
// Prettify the error message.
1312+
// Only keep the 2nd line of the stack trace:
1313+
// - First line should be this file (index.js)
1314+
// - Second line should be the Webpack config file
1315+
const pe = new PrettyError();
1316+
pe.skip((traceLine, lineNumber) => lineNumber !== 1);
13051317
const error = new Error(errorMessage);
1306-
console.log(new PrettyError().render(error));
1318+
console.log(pe.render(error));
13071319
process.exit(1); // eslint-disable-line
13081320
}
13091321

lib/package-helper.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ function ensurePackagesExist(packagesConfig, requestedFeature) {
1818
const missingPackagesRecommendation = getMissingPackageRecommendations(packagesConfig, requestedFeature);
1919

2020
if (missingPackagesRecommendation) {
21-
throw new Error(`
21+
throw `
2222
${missingPackagesRecommendation.message}
2323
${missingPackagesRecommendation.installCommand}
24-
`
25-
);
24+
`;
2625
}
2726

2827
// check for invalid versions & warn

test/bin/encore.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,41 @@ module.exports = Encore.getWebpackConfig();
193193
done();
194194
});
195195
});
196+
197+
it('Display an error when calling an unknown method', (done) => {
198+
testSetup.emptyTmpDir();
199+
const testDir = testSetup.createTestAppDir();
200+
201+
fs.writeFileSync(
202+
path.join(testDir, 'package.json'),
203+
`{
204+
"devDependencies": {
205+
"@symfony/webpack-encore": "*"
206+
}
207+
}`
208+
);
209+
210+
fs.writeFileSync(
211+
path.join(testDir, 'webpack.config.js'),
212+
`
213+
const Encore = require('../../index.js');
214+
Encore
215+
.setOutputPath('build/')
216+
.setPublicPath('/build')
217+
.enableSingleRuntimeChuck()
218+
.addEntry('main', './js/no_require')
219+
;
220+
221+
module.exports = Encore.getWebpackConfig();
222+
`
223+
);
224+
225+
const binPath = path.resolve(__dirname, '../', '../', 'bin', 'encore.js');
226+
exec(`node ${binPath} dev --context=${testDir}`, { cwd: testDir }, (err, stdout, stderr) => {
227+
expect(err).not.to.be.null;
228+
expect(stdout).to.contain('is not a recognized property or method');
229+
expect(stdout).to.contain('Did you mean');
230+
done();
231+
});
232+
});
196233
});

0 commit comments

Comments
 (0)