Skip to content

Commit 9d8992e

Browse files
committed
minor #541 Fix the color of error messages when they are displayed on a light theme (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Fix the color of error messages when they are displayed on a light theme This PR fixes #533 by slightly modifying the default theme of `pretty-error` so it doesn't use a color for the error message. **Before:** ![image](https://user-images.githubusercontent.com/850046/54559916-6dbb0680-49c1-11e9-963c-946495788315.png) **After:** ![image](https://user-images.githubusercontent.com/850046/54559933-76134180-49c1-11e9-919e-61e69ac6cfe2.png) Commits ------- 3c5258b Fix error colors when displayed on a light theme
2 parents d2622d0 + 3c5258b commit 9d8992e

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

index.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
const WebpackConfig = require('./lib/WebpackConfig');
1313
const configGenerator = require('./lib/config-generator');
1414
const validator = require('./lib/config/validator');
15-
const PrettyError = require('pretty-error');
15+
const prettyError = require('./lib/utils/pretty-error');
1616
const logger = require('./lib/logger');
1717
const parseRuntime = require('./lib/config/parse-runtime');
1818
const chalk = require('chalk');
@@ -1320,10 +1320,7 @@ const EncoreProxy = new Proxy(new Encore(), {
13201320
const res = target[prop](...parameters);
13211321
return (res === target) ? EncoreProxy : res;
13221322
} catch (error) {
1323-
// prettifies errors thrown by our library
1324-
const pe = new PrettyError();
1325-
1326-
console.log(pe.render(error));
1323+
prettyError(error);
13271324
process.exit(1); // eslint-disable-line
13281325
}
13291326
};
@@ -1356,10 +1353,11 @@ const EncoreProxy = new Proxy(new Encore(), {
13561353
// Only keep the 2nd line of the stack trace:
13571354
// - First line should be this file (index.js)
13581355
// - Second line should be the Webpack config file
1359-
const pe = new PrettyError();
1360-
pe.skip((traceLine, lineNumber) => lineNumber !== 1);
1361-
const error = new Error(errorMessage);
1362-
console.log(pe.render(error));
1356+
prettyError(
1357+
new Error(errorMessage),
1358+
{ skipTrace: (traceLine, lineNumber) => lineNumber !== 1 }
1359+
);
1360+
13631361
process.exit(1); // eslint-disable-line
13641362
}
13651363

lib/utils/pretty-error.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const PrettyError = require('pretty-error');
13+
14+
/**
15+
* Render a pretty version of the given error.
16+
*
17+
* Supported options:
18+
* * {function} skipTrace
19+
* An optional callback that defines whether
20+
* or not each line of the eventual stacktrace
21+
* should be kept. First argument is the content
22+
* of the line, second argument is the line number.
23+
*
24+
* @param {*} error
25+
* @param {object} options
26+
*
27+
* @returns {void}
28+
*/
29+
module.exports = function(error, options = {}) {
30+
const pe = new PrettyError();
31+
32+
// Use the default terminal's color
33+
// for the error message.
34+
pe.appendStyle({
35+
'pretty-error > header > message': { color: 'none' }
36+
});
37+
38+
// Allow to skip some parts of the
39+
// stacktrace if there is one.
40+
if (options.skipTrace) {
41+
pe.skip(options.skipTrace);
42+
}
43+
44+
console.log(pe.render(error));
45+
};

0 commit comments

Comments
 (0)