floatdrop / gulp-plumber

Fixing Node pipes
MIT License
806 stars 32 forks source link

How to beep on error but keep error message informative #38

Closed NeXTs closed 6 years ago

NeXTs commented 8 years ago

There a lot of instructions on the web regarding how to extend errorHandler method to beep and show notification on error, but all those methods breaks the basic idea of plumber: tiny and informative error message.

Take a look to difference between default error message great_error_msg

and error message that usually comes with extended errorHandler method poor_error_msg

One of extended errorHandler implementation

var onError = function(err) {
    notify.onError({
        title:    "Error!",
        message:  "<%= error.message %>",
        sound:    "Beep"
    })(err);
    this.emit('end');
};

gulp.task('scripts', function() {
    return gulp.src(src + '/js/**/*.js')
    .pipe(plumber({ errorHandler: onError }))
    ...

Default error message shows the most important information, like path to file, line, error message, even sample of code from that line. While extended message is much worse, it shows only error message without additional information.

On the other hand it's not convenient to code without notification in case of error. In my case console is almost always hidden so I can not notice if it crashes.

As I said, there are many attempts on the web to solve this problem, but they all are not good enough.

I would be very grateful to you if you've added the ability to beep and display notification on error by default. Or at least show us how to extend the errorHandler method to make it display error messages in console such informative and beautifully as it is by defalt

NeXTs commented 8 years ago

Ohh, sure :)

console.log(err.toString());
NeXTs commented 8 years ago

Well, I achieved what I wanted, maybe it will be helpful for someone.

var notify = require('gulp-notify');
var beep = require('beepbeep');

var onError = function(err) {
    notify.onError({
      title:    "Gulp error in " + err.plugin,
      message:  err.toString()
    })(err);
    beep(3);
    this.emit('end');
};

gulp.task('scripts', function() {
    return gulp.src(src + '/js/**/*.js')
    .pipe(plumber({ errorHandler: onError }))
    ...
kapena commented 8 years ago

This looks like a really neat solution @NeXTs I'm thinking of using in my build. Having an issue with plumber atm as it's not responding upon fixes to my errors. Check out my SO post on the issue that I'm having.

kapena commented 8 years ago

So I used your solution @NeXTs and also refactored my gulpfile while I was at it. However I don't think your onError function is working...Not getting beeps inside terminal and the error heading that I'm getting it not "Gulp error in".

Not sure what's really wrong here..Looks all good to me..Can you take a look.

var notify = require('gulp-notify');
var beep = require('beepbeep');

// error handeler function
var onError = function(err){
    notify.onError({
        title: "gulp error in " + err.plugin,
        message: err.toString()
    })(err);
    beep(3);
    this.emit('end');
};

// styles task. 
gulp.task('styles', function(){
    gulp.src(paths.source.styles)
        .pipe(plumber({
            // plumber finds errors in stream
            errorHandeler:onError }))
        .pipe(sass())
         // compile sass
        .pipe(gulp.dest(paths.destination.styles))
        .pipe(cssmin()) // min css
        .pipe(rename({ // rename file to site.min.css
            suffix:'.min'
        }))
        .pipe(gulp.dest(paths.destination.styles))
        .pipe(browserSync.stream());

});

Here's the what I am getting in terminal.. Error in plugin 'gulp-sass' Message: src/scss/partials/home/_header.scss 16:18 style declaration must contain a value

NeXTs commented 8 years ago

@kapena I don't get what you were expecting? You got error in terminal as was expected.. To display error notification in the tray and beep you have to install gulp-notify and beepbeep plugins as well. With following settings gulp should continue watching for changes and process tasks even after fail.