How to use the PluginError function from gulp-util

Find comprehensive JavaScript gulp-util.PluginError code examples handpicked from public code repositorys.

gulp-util.PluginError is a constructor function in the Gulp.js utility library that creates a new error object with a message and plugin name.

8
9
10
11
12
13
14
15
16
17

var gutil = require('gulp-util'),
    path = require('path'),
    through = require('through2');

var PluginError = gutil.PluginError;

var resourceUtil = require('./resource');

/**
fork icon195
star icon457
watch icon57

+ 3 other calls in file

15
16
17
18
19
20
21
22
23

"use strict";

const through = require('through2');
const gutil = require('gulp-util');
const PluginError = gutil.PluginError;
const Templates = require('./lib/Templates');
const Metadata = require('./lib/Metadata');
const ExampleFile = require('./lib/ExampleFile');
fork icon530
star icon3
watch icon11

+ 3 other calls in file

How does gulp-util.PluginError work?

gulp-util.PluginError works by creating a new PluginError object, which is used to represent an error that occurred in a Gulp.js plugin. When called with a message and a plugin name, gulp-util.PluginError creates a new PluginError object with the following properties: message: a string representing the error message. plugin: a string representing the name of the plugin that caused the error. name: a string representing the name of the error type ("PluginError"). The resulting PluginError object can then be thrown or passed to a callback function, in order to propagate the error to other parts of the program. The gulp-util library is a set of utility functions and objects for working with Gulp.js, a popular task runner for JavaScript projects. PluginError is a common object used in Gulp.js plugins, and can be used to represent any kind of error or exception that occurs during a Gulp.js task. Note that gulp-util.PluginError is part of the gulp-util library, which has been deprecated as of Gulp.js version 4. However, similar functionality is provided by the plugin-error package, which can be used to create error objects in Gulp.js plugins.

10
11
12
13
14
15
16
17
18
19
  this.push(file);
  return cb();
}

if (file.isStream()) {
  this.emit('error', new gutil.PluginError('cmd2commonjs', 'Streaming not supported'));
}

try {
  gutil.log(file.path);
fork icon26
star icon140
watch icon7

+ 7 other calls in file

96
97
98
99
100
101
102
103
104
105
    return cb()
}

// 插件不支持对stream直接操作,抛出异常
if (file.isStream()) {
    this.emit('error', new util.PluginError('generateScript', 'Streaming not supported'))
    return cb()
}

// 内容转换,处理好后,再转成 Buffer 形式
fork icon884
star icon0
watch icon150

+ 23 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const PluginError = require("gulp-util").PluginError;

const PLUGIN_NAME = "my-gulp-plugin";

function myGulpTask() {
  try {
    // do some work
  } catch (err) {
    throw new PluginError(PLUGIN_NAME, err.message);
  }
}

In this example, we first import the gulp-util library and assign the PluginError constructor function to a variable for convenience. We then define a Gulp.js task called myGulpTask, which does some work and catches any errors that occur during the task. If an error occurs, we create a new PluginError object using the PluginError constructor function, passing in the name of our plugin (PLUGIN_NAME) and the error message. The resulting PluginError object is then thrown, which will propagate the error to any other parts of the program that are listening for errors in this task. Note that this is just a simple example, and gulp-util.PluginError can be used in more complex scenarios, such as building custom Gulp.js plugins that need to generate error objects with more detailed information or custom properties.

102
103
104
105
106
107
108
109
110
111
if (file.isNull()) {
    cb();
    return;
}
if (file.isStream()) {
    return cb(new gutil.PluginError('gulp-typescript', 'Streaming not supported'));
}
var inputFile = this.project.input.addGulp(file);
this.project.compiler.inputFile(inputFile);
cb();
fork icon135
star icon0
watch icon2

34
35
36
37
38
39
40
41
42
43
if (file.isNull()) {
    cb();
    return;
}
if (file.isStream()) {
    return cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
var isFirstFile = this.project.input.firstSourceFile === undefined;
var inputFile = this.project.input.addGulp(file);
if (isFirstFile) {
fork icon135
star icon0
watch icon5

15
16
17
18
19
20
21
22
23
24
  this.push(file);
  return cb();
}

if (file.isStream()) {
  this.emit('error', new gutil.PluginError('svg-sprite', 'Streaming not supported'));
}

try {
  gutil.log(file.path);
fork icon123
star icon997
watch icon31

+ 7 other calls in file

24
25
26
27
28
29
30
31
32
33
        cb(null, file);
        return;
}

if (file.isStream()) {
        cb(new gutil.PluginError('gulp-babel', 'Streaming not supported'));
        return;
}

try {
fork icon133
star icon0
watch icon2

+ 3 other calls in file

136
137
138
139
140
141
142
143
144
145
- Error properties will be included in `err.toString()`. Can be omitted by including `{showProperties: false}` in the options.

These are all acceptable forms of instantiation:

```javascript
var err = new gutil.PluginError('test', {
  message: 'something broke'
});

var err = new gutil.PluginError({
fork icon104
star icon0
watch icon2

+ 9 other calls in file

3
4
5
6
7
8
9
10
11
12
    useref = require('node-useref');

module.exports = function () {
    return through.obj(function (file, enc, cb) {
        if (file.isStream()) {
            this.emit('error', new gutil.PluginError('gulp-useref', 'Streaming not supported'));
            return cb();
        }

        var output = useref(file.contents.toString());
fork icon93
star icon0
watch icon2

+ 5 other calls in file

44
45
46
47
48
49
50
51
52
53
  options.extension = '.' + options.type;
}

var stream = through2.obj(function (file, enc, done) {
  var fail = function (message) {
    return done(new gutil.PluginError('raml2html', message));
  };

  if (file.isStream()) {
    return fail('Streams are not supported: ' + file.inspect());
fork icon4
star icon5
watch icon9

39
40
41
42
43
44
45
46
47
48
            cb(null, file);
            return;
    }

    if (file.isStream()) {
            cb(new gutil.PluginError('gulp-pegjs', 'Streaming not supported'));
            return;
    }

// always generate source code of parser
fork icon36
star icon0
watch icon1

+ 3 other calls in file

15
16
17
18
19
20
21
22
23
24

const presets = ['babel-preset-es2015', 'babel-preset-es2016', 'babel-preset-es2017', 'babel-preset-stage-2'].map(require.resolve);

gulp.task('prod', () => {
  webpack(builds.production, (err, stats) => {
    if (err) throw new gutil.PluginError('webpack:build', err);
    gutil.log('[webpack:build]', stats.toString({
      colors: true,
    }));
  });
fork icon5
star icon7
watch icon7

+ 7 other calls in file

17
18
19
20
21
22
23
24
25
26
var attribute = options.attribute || 'src';
var getHTTP = options.getHTTP || false;

return through.obj(function(file, encoding, callback){
	if(file.isStream()){
		this.emit('error', new util.PluginError(PLUGIN_NAME, 'Streams are not supported!'));
		return callback();
	}

	if(file.isBuffer()){
fork icon7
star icon3
watch icon0

+ 20 other calls in file

204
205
206
207
208
209
210
211
212
213
return function(done) {
    const config = require('./webpack.config')(webpackOptions);

    webpack(config, function(err, stats) {
        if (err) {
            throw new gutil.PluginError('webpack', err);
        }
        gutil.log('[webpack]', stats.toString({ maxModules: Infinity, colors: true, optimizationBailout: true }));
        if (typeof done !== 'undefined' && (!opts || !opts.watch)) {
            done();
fork icon1
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
  dest: 'assets/js',
  dependenciesPath: './'
};

if (typeof options.dest == 'undefined') {
  return done(new gutil.PluginError(PLUGIN_NAME, 'Please provide source path...'));
}

if (typeof options.dest == 'undefined') {
  options.dest = '';
fork icon0
star icon0
watch icon1

+ 5 other calls in file

74
75
76
77
78
79
80
81
82
    cb( null, outFile );
  };

  gutil.log('RequireJS optimizing');
  requirejs.optimize( options, null, function( err ) {
    var gulpError = new gutil.PluginError( 'requirejsOptimize', err.message );
    stream.emit( 'error', gulpError );
  });
});
fork icon0
star icon0
watch icon0

4
5
6
7
8
9
10
11
12
13
    through = require('through2'),
    Buffer = require('buffer').Buffer,
    IMPORT_REGX = /\/\*#inport[\s]*(['<])[\s]*(.+)[\s]*['>][\s]*\*\/[\r\n]*/g;

var PluginError = function(message) {
  return new gutil.PluginError('gulp-js-include', message);
};

module.exports = function(paths) {
  var files = [];
fork icon0
star icon0
watch icon2