-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulp-prettyerror.js
executable file
·67 lines (53 loc) · 2.4 KB
/
gulp-prettyerror.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const _logger = require('gulplog');
const _colors = require('ansi-colors');
const _gplumber = require('gulp-plumber');
// Main Function
module.exports = function PrettyError(customErrorFormat){
// custom error format function provided ?
if (typeof customErrorFormat != 'undefined'){
// proxy
return _gplumber(function(error){
// run custom error handler/output formatting
customErrorFormat.apply(this, [error, _logger]);
// make sure the process is finished
this.emit('end');
});
}else{
// default appearance
return _gplumber(function(error){
// extract values and apply defaults
const plugin = error.plugin || 'unknown';
let message = error.message || 'unknown error';
const codeFrame = error.codeFrame || null;
const cause = error.cause || {};
// detailed message given ? append it
if (cause.message){
const file = cause.filename || 'unknown file';
const line = cause.line || '0';
const position = cause.position || '0';
// generate detailed error message
message = '[' + file + '] - ' + cause.message + ' (' + line + ':' + position + ')';
}
// log the error message
_logger.error('|- ' + _colors.bgRed.bold('Build Error in ' + plugin));
_logger.error('|- ' + _colors.bgRed.bold(message));
// make sure there is codeFrame in the error object
if (codeFrame){
// add indentation
const msg = codeFrame.replace(/\n/g, '\n ');
_logger.error('|- ' + _colors.bgRed('>>>'));
_logger.error('|\n ' + msg + '\n |');
_logger.error('|- ' + _colors.bgRed('<<<'));
// stacktrace available ?
}else if (cause.stack){
// add indentation
const stacktrace = cause.stack.replace(/^(\s*)/gm, ' | ');
_logger.error('|- ' + _colors.bgRed('>>>'));
_logger.error('|\n' + stacktrace + '\n |');
_logger.error('|- ' + _colors.bgRed('<<<'));
}
// make sure the process is finished
this.emit('end');
});
}
}