What is plugin-error?
The plugin-error npm package is designed to simplify error handling in plugin development, particularly for Gulp plugins. It provides a consistent way to create and manage errors, making it easier to debug and handle errors in a standardized manner.
What are plugin-error's main functionalities?
Creating a PluginError
This feature allows you to create a new PluginError instance. The first argument is the plugin name, and the second argument is the error message. This helps in identifying which plugin caused the error.
const PluginError = require('plugin-error');
const err = new PluginError('MyPlugin', 'Something went wrong!');
console.error(err.toString());
Handling Error Options
This feature allows you to pass an options object to the PluginError constructor. The options can include properties like 'message' and 'showStack' to customize the error output.
const PluginError = require('plugin-error');
const err = new PluginError('MyPlugin', {
message: 'Something went wrong!',
showStack: true
});
console.error(err.toString());
Using Error Codes
This feature allows you to specify an error code, which can be useful for programmatically identifying different types of errors.
const PluginError = require('plugin-error');
const err = new PluginError('MyPlugin', {
message: 'Something went wrong!',
code: 'EPLUGIN'
});
console.error(err.toString());
Other packages similar to plugin-error
fancy-log
The fancy-log package is used for logging messages in a more readable format. While it doesn't specifically handle errors, it can be used alongside plugin-error to log error messages in a more user-friendly way.
gulplog
The gulplog package is a logging utility specifically designed for Gulp. It provides a way to log messages at different levels (info, warn, error). While it doesn't create error objects like plugin-error, it can be used to log errors created by plugin-error.
chalk
The chalk package is used for styling terminal string output. It can be used in conjunction with plugin-error to colorize error messages, making them easier to read and debug.
plugin-error
Error handling for Vinyl plugins.
Usage
var PluginError = require('plugin-error');
var err = new PluginError('test', {
message: 'something broke',
});
var err = new PluginError({
plugin: 'test',
message: 'something broke',
});
var err = new PluginError('test', 'something broke');
var err = new PluginError('test', 'something broke', { showStack: true });
var existingError = new Error('OMG');
var err = new PluginError('test', existingError, { showStack: true });
API
new PluginError(pluginName, message[, options])
Error constructor that takes:
pluginName
- a String
that should be the module name of your pluginmessage
- a String
message or an existing Error
objectoptions
- an Object
of your options
Behavior:
- By default the stack will not be shown. Set
options.showStack
to true if you think the stack is important for your error. - If you pass an error object as the message the stack will be pulled from that, otherwise one will be created.
- If you pass in a custom stack string you need to include the message along with that.
- Error properties will be included in
err.toString()
, but may be omitted by including { showProperties: false }
in the options.
License
MIT