問題描述
我正在創(chuàng)建一個在某些情況下可能會失敗的 gulp 任務(wù).
gulp.task('favicon', function () {嘗試 {require('child_process').execSync('icotool --version');} 捕捉( e ) {var err = new Error('生成 favicon 需要 Unix bash 和 icotool');拋出錯誤;}返回 gulp.src('', {read: false}).管道(外殼(['./generate-favicon.sh']));});
當(dāng)通過 gulp 運(yùn)行我的任務(wù)并遇到錯誤時,該錯誤將呈現(xiàn)得相當(dāng)難看.我想以一種方式呈現(xiàn)錯誤,例如,jslint gulp-util 的 PluginError
.
它實際上只是在其中創(chuàng)建一個 PluginError
并拋出它,但這似乎不太正確.另一個不太好的解決方案是設(shè)置
err.showStack = false;
至少有更好的錯誤輸出.gulp.task.Error
會很好.
據(jù)我所知,從 gulp 中拋出錯誤并不是很好.但是我發(fā)現(xiàn)了這個我曾經(jīng)為我工作的博客條目.
http://gotofritz.net/博客/geekery/how-to-generate-error-in-gulp-task/
編輯:gulp-util
已棄用.相反,請使用 plugin-error
包.p>
我的例子:
var gulp = require('gulp');var error = require('plugin-error');gulp.task('部署',函數(shù)(cb){if(typeof(specialId) === '未定義') {var err = new PluginError({插件:'部署',消息:'specialId 為空.'});}}
I am creating a gulp task which might fail under certain circumstances.
gulp.task('favicon', function () {
try {
require('child_process').execSync('icotool --version');
} catch( e ) {
var err = new Error( 'Unix bash and icotool required for generating favicon' );
throw err;
}
return gulp.src('', {read: false})
.pipe(shell([
'./generate-favicon.sh'
]));
});
When running my task via gulp and running into the error, the error will be presented rather ugly.
I would like to present the error in a way as it is done by e.g. jslint gulp-util's PluginError
.
It actually works to just create a PluginError
there and throw it but that doesn't seem quite right.
Another solution not that nice would be to set
err.showStack = false;
for at least a little nicer error output. A gulp.task.Error
would be nice.
From what I've seen its not great to throw an error from gulp. But I found this blog entry that I used to work for me.
http://gotofritz.net/blog/geekery/how-to-generate-error-in-gulp-task/
Edit: gulp-util
has been deprecated. Instead, use the plugin-error
package.
My Example:
var gulp = require('gulp');
var error = require('plugin-error');
gulp.task('deploy', function(cb) {
if(typeof(specialId) === 'undefined') {
var err = new PluginError({
plugin: 'deploy',
message: 'specialId is empty.'
});
}
}
這篇關(guān)于在 gulp 任務(wù)中很好地拋出錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!