使用 gulp-notify 和 gulp-plumber 处理错误 [英] Error handling with gulp-notify and gulp-plumber

查看:21
本文介绍了使用 gulp-notify 和 gulp-plumber 处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下情况下显示通知:
1.出现错误
2.脚本执行成功时

I want to display a notification when:
1. An error occurs
2. When the script was successfully executed

所有通知实际上都在工作,但始终显示成功通知,即使出现错误也是如此.

All notifications are actually working but the success notification is always displayed, even when there is an error.

如何确保只有在没有发生错误时才显示成功通知?

How can I make sure the success notification is only displayed when no errors have occurred?

这是我目前的任务:

var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var rename = require('gulp-rename');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var minifycss = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');

gulp.task('sass', function(){
   return gulp.src('assets/scss/global.scss')
       .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
       .pipe(sass({ style: 'expanded' }))
       .pipe(autoprefixer())
       .pipe(gulp.dest('assets/css/'))
       .pipe(rename({suffix: '.min'}))
       .pipe(minifycss())
       .pipe(gulp.dest('assets/css/'))
       .pipe(notify({
           title: 'Gulp',
           subtitle: 'success',
           message: 'Sass task',
           sound: "Pop"
       }));
});

gulp.task('watch', function(){
   gulp.watch('assets/scss/**/*.scss', ['sass']);
});

gulp.task('default', ['sass', 'watch']);

推荐答案

我遇到了同样的问题,对我有用的是 gulp-if 和自定义错误处理程序.基本上,我在抛出错误时设置 errorFree = false 然后在最后显示成功消息时我使用 gulp-if 来确定是否 notify() 是否被调用.

I encountered the same issue and what worked for me was gulp-if and a custom error handler. Basically, I set errorFree = false when an error is thrown then at the end when it's time to show the success message I use gulp-if to determine if notify() gets called or not.

使用您的 gulpfile:

Working with your gulpfile:

var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var rename = require('gulp-rename');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var minifycss = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');

gulp.task('sass', function(){

    var onError = function(err) {
        notify.onError({
                    title:    "Gulp",
                    subtitle: "Failure!",
                    message:  "Error: <%= error.message %>",
                    sound:    "Beep"
                })(err);

        this.emit('end');
    };

   return gulp.src('assets/scss/global.scss')
       .pipe(plumber({errorHandler: onError}))
       .pipe(sass({ style: 'expanded' }))
       .pipe(autoprefixer())
       .pipe(gulp.dest('assets/css/'))
       .pipe(rename({suffix: '.min'}))
       .pipe(minifycss())
       .pipe(gulp.dest('assets/css/'))
       .pipe(notify({ // Add gulpif here
           title: 'Gulp',
           subtitle: 'success',
           message: 'Sass task',
           sound: "Pop"
       }));
});

gulp.task('watch', function(){
   gulp.watch('assets/scss/**/*.scss', ['sass']);
});

gulp.task('default', ['sass', 'watch']);

在我的 gulpfile 中,我实际上将每个步骤包装在 gulp-if 中,以便流停止处理.(我还没有找到在不终止进程的情况下停止流的方法,这违背了观看 IMO 的目的.)

In my gulpfile I actually wrap each step in gulp-if so that the stream stops processing. (I haven't found a way to stop the stream without killing the process, which defeats the purpose of watch IMO.)

这篇关于使用 gulp-notify 和 gulp-plumber 处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆