How to use the onError function from gulp-notify

Find comprehensive JavaScript gulp-notify.onError code examples handpicked from public code repositorys.

gulp-notify.onError is a function in the gulp-notify plugin that allows developers to handle errors that occur during a Gulp task and display a notification to the user.

83
84
85
86
87
88
89
90
91
92

var data;
try {
    data = JSON.parse(output);
} catch (e) {
    notify.onError('<%= error.message %>').call(new Buffer(''), e);
    return callback();
}

var endTime = data.testResults.length ? data.testResults
fork icon1
star icon8
watch icon0

+ 19 other calls in file

34
35
36
37
38
39
40
41
42
43
}

const infoLogs = stripIndent(
  data.map(({ message, code, title, type }, i) => {
    if (type !== "Warning") {
      notify.onError({
        title,
        message,
      })();
    }
fork icon0
star icon2
watch icon0

+ 3 other calls in file

How does gulp-notify.onError work?

gulp-notify.onError is a function provided by the gulp-notify plugin that allows developers to handle errors that occur during a Gulp task and display a notification to the user.

When a Gulp task encounters an error, the onError function will be called with the error object as its argument. The function can then be used to display a notification to the user, log the error to the console, or perform other actions as needed.

To use gulp-notify.onError, developers can add the function as a callback to their Gulp task using the .pipe() method. For example, a Gulp task for compiling Sass and handling errors might look like this:

javascript
const gulp = require('gulp'); const sass = require('gulp-sass'); const notify = require('gulp-notify'); gulp.task('sass', () => { return gulp.src('src/scss/*.scss') .pipe(sass().on('error', notify.onError((error) => { return { title: 'Sass Error', message: error.message }; }))) .pipe(gulp.dest('dist/css')); });

In this example, we define a Gulp task that uses the gulp-sass plugin to compile Sass files. If an error occurs during the compilation process, the onError function is called with the error object as its argument.

The onError function uses gulp-notify to display a notification to the user indicating that a Sass error has occurred. The notification includes a title of "Sass Error" and the error message from the error object.

By using gulp-notify.onError, developers can provide more informative error messages and improve the usability of their Gulp tasks, making it a valuable tool for many Gulp-based projects.

44
45
46
47
48
49
50
51
52
53
54
 *   title: string,
 * }}
 * @return {function(msg: string | NotifierParams)}
 */
const fabricLogger = ({ type, title }) => (msg) => {
  const notifier = type === 'error' ? notify.onError.bind(notify) : notify;
  const isString = typeof msg == 'string';


  return notifier({
    onLast: true,
fork icon0
star icon0
watch icon1

+ 24 other calls in file

86
87
88
89
90
91
92
93
94
95
96
97
})




gulp.task('js:dev', () => {
  return gulp.src(paths.scripts)
    .pipe(plumber({errorHandler: notify.onError('Error:<%=error.message%>')}))
    .pipe(babel({
      presets: ['@babel/preset-env']
    }))
    .pipe(gulp.dest('./dist/js'))
fork icon0
star icon0
watch icon0

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const gulp = require("gulp");
const concat = require("gulp-concat");
const uglify = require("gulp-uglify");
const notify = require("gulp-notify");

gulp.task("scripts", () => {
  return gulp
    .src("src/js/*.js")
    .pipe(concat("all.js"))
    .pipe(
      uglify().on(
        "error",
        notify.onError((error) => {
          return {
            title: "JavaScript Error",
            message: error.message,
          };
        })
      )
    )
    .pipe(gulp.dest("dist/js"))
    .on("error", console.error.bind(console));
});

In this example, we define a Gulp task that concatenates and minifies JavaScript files. If an error occurs during the minification process, the onError function is called with the error object as its argument. The onError function uses gulp-notify to display a notification to the user indicating that a JavaScript error has occurred. The notification includes a title of "JavaScript Error" and the error message from the error object. Additionally, the error is logged to the console using console.error(). By using gulp-notify.onError, developers can provide more informative error messages and improve the usability of their Gulp tasks, making it a valuable tool for many Gulp-based projects.

107
108
109
110
111
112
113
114
115
116
function compileGlobalThemeSCSS(done) {
  gulp
    .src(SRC.SCSS_THEME)
    .pipe(sourcemaps.init())
    .pipe(
      plumber({ errorHandler: notify.onError('Error: <%= error.message %>') })
    )
    .pipe(sass.sync({ outputStyle: cssOutputStyle }))
    .pipe(autoprefixer())
    .pipe(gulpif(shouldMinify, gcmq()))
fork icon0
star icon0
watch icon0

+ 39 other calls in file

86
87
88
89
90
91
92
93
94
95
    'src/js/components/**/*.js',
    'src/js/main.js',
  ])
    .pipe(gulpif(!isProd, sourcemaps.init()))
    .pipe(concat('main.js'))
    .pipe(gulpif(isProd, uglify().on('error', notify.onError())))
    .pipe(gulpif(!isProd, sourcemaps.write()))
    .pipe(dest(!isProd ? 'dist/js' : 'public/js'))
    .pipe(browserSync.stream());
}
fork icon0
star icon0
watch icon0

+ 2 other calls in file

99
100
101
102
103
104
105
106
107
108
 * Основные стили проекта сборка из SCSS
 */
gulp.task('scss', () => {
    return gulp.src(paths.src.scss + 'main.scss')
        .pipe(plumber({
            errorHandler: notify.onError(err => ({
                title: err.plugin,
                message: err.message
            }))
        }))
fork icon0
star icon0
watch icon0

+ 2 other calls in file

34
35
36
37
38
39
40
41
42
43
gulp.task('html', function() {
  return gulp
      .src(['**/*.pug', '!**/_*.pug'], {cwd: `src/${serveDir}`})
      .pipe(
          plumber({
            errorHandler: notify.onError((err) => ({
              title: 'HTML',
              message: err.message,
            })),
          }),
fork icon0
star icon0
watch icon0

+ 4 other calls in file