如何在Gulp任务错误回调中不显示堆栈跟踪?

时间:2016-08-23 05:14:23

标签: node.js gulp

当我在其中一项任务中出错时:

gulp.task('foo', function(onDone) {
    onDone('It did not work');
});

我得到了一个无关紧要的堆栈跟踪:

[13:10:25] 'foo' errored after 4.27 s
[13:10:25] Error: It did not work
    at formatError (C:\npm\node_modules\gulp\bin\gulp.js:169:10)
    at Gulp.<anonymous> (C:\npm\node_modules\gulp\bin\gulp.js:195:15)
    at emitOne (events.js:96:13)
    at Gulp.emit (events.js:188:7)
    at Gulp.Orchestrator._emitTaskDone (D:\Code\my-project\node_modules\orchestrator\index.js:264:8)
    at D:\Code\my-project\node_modules\orchestrator\index.js:275:23
    at finish (D:\Code\my-project\node_modules\orchestrator\lib\runTask.js:21:8)
    at cb (D:\Code\my-project\node_modules\orchestrator\lib\runTask.js:29:3)
    at D:\Code\my-project\gulp-tasks\insert-docs.js:116:25
    at Request._callback (D:\Code\my-project\gulpfile.js:112:17)

我怎么不显示?我更喜欢使用in build gulp功能而不是第三方模块或console.error()

1 个答案:

答案 0 :(得分:2)

传递给回调的Error个对象上的gulp checks for the presence of a showStack property。这可能是为了支持gutil.PluginError上的选项而引入的,但可以与任何Error对象一起使用:

function withError(msg) {
  var err = new Error(msg);
  err.showStack = false;
  return err;
}

gulp.task('foo', function(done) {
  done(withError('It did not work'));
});