Error handling with gulp-notify and gulp-plumber

2020-06-14 19:49发布

问题:

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?

This is my current task:

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']);

回答1:

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.

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']);

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.)



回答2:

I've run into a very similar situation recently and the code Jeffwa provided helped me make it work; but I'm compiling and merging several files, and was having issues with the error and the "success" notification being fired.

In case someone else run into a similar situation the solution I found was creating a function on the success notification and there returning either false if there was an error or the information object. This is the code example.

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

    var errorFree = true;

    var onError = function(err) {

        errorFree = false;

        var subtitle = "Error";
        var message = error.message;

        notify.onError({
            title:    "Gulp",
            subtitle: subtitle,
            message:  message,
            sound: false
        })(err);

        this.emit('end');
    };

    //themeCssFiles is an array with the css files to be compiled/merged.
    return gulp.src( themeCssFiles )
        .pipe( plumber({
            errorHandler: onError,
        }) )
        .pipe( gulpIf("*.scss", compass({
            config_file: './compass-config.rb',
            sass: 'scss',
            css: outputCSS + "/compass"
        }) ) )
        .pipe( concatCss('theme.css', {
            rebaseUrls: false
        } ) )
        .pipe( notify( function(f) {
            return errorFree ? {
                title: 'Gulp',
                subtitle: 'success',
                message: 'SCSS ready',
            }
            : false ;
        }))
        .pipe( gulp.dest( 'dist/css' ) );
});


标签: gulp