Search code examples
error-handlinggulpgulp-notify

gulp-notify - get file name, not whole path, with error


I'm using gulp-notify and gulp-plumber to catch any errors from gulp-sass and notify me before it throws an error in gulp and stops the whole build process. However, the error shown with gulp-notify isn't particularly helpful...it contains the whole path to the file with the error; I will most likely know what the path to the file is, so I only really to need know the file name and line number (and column number as an added but not necessary bonus), and also the actual error message. Here is my code:

gulp.task('styles', function () {
    var onError = function(err) {
      notify.onError({
        title:    "Gulp",
        subtitle: "Build Error!",
        message:  "<%= error.message %>",
        sound:    "Beep"
      })(err);
      this.emit('end');
    };
    gulp.src('assets/styles/source/style.scss')
        .pipe(plumber({errorHandler: onError}))
        .pipe(sass({
            style: 'compressed',
            errLogToConsole: false
         }))
        .pipe(gulp.dest('assets/styles/build'))
        .pipe(autoprefixer({
            browsers: ['last 2 versions', 'ie 9', 'ios 6', 'android 4'],
            cascade: false
        }))
        .pipe(gulp.dest('assets/styles/build'))
        .pipe(minifyCSS({
            keepSpecialComments: '*'
        }))
        .pipe(gulp.dest('./'))
        .pipe(reload({stream: true}));
});

There's more (like gulp-watch, browsersync, some JS stuff) but I think the above is all that's needed to solve the issue. Let me know if not!

So my question is, how do I modify the error message to only show me the file name, line number and error message (e.g _header.scss:17 - invalid property name)? Currently I'm using error.message but I can't find any more tags I can use to get the things I want to show.

Thank you!


Solution

  • Here's how I solved that problem:

    .pipe(sass(
        ({
          style: 'compressed',
          errLogToConsole: false,
          onError: function(error) {
            notify({
              title: "SASS ERROR",
              message: "line " + error.line + " in " + error.file.replace(/^.*[\\\/]/, '') + "\n" + error.message
            }).write(error);
          }
        })
      )
    )
    

    I also noticed that I'm only able to provide one line break "\n" so I wasn't able to split each piece of information I wanted on its own line.