verror
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
assert-plus | 1.0.0 | 3.85 kB | MIT | prod | |
core-util-is | 1.0.2 | 6.85 kB | MIT | prod | |
extsprintf | 1.4.1 | 10.72 kB | MIT | prod |
Verror is a Node.js library that enhances and enriches the capability of JavaScript in handling errors. This library offers various classes that support printf-style arguments for easier message readability, chains of causes that enable tracking of errors from initial point to the final error point, extra properties to supply more information about the error, and the creation of personalized subclasses that come with full support.
To use verror, first, install the package using npm:
npm install verror
Verror can be used as a replacement for JavaScript Error class. It offers additional features such as printf-style messages:
var VError = require('verror');
var err = new VError('missing file: "%s"', '/etc/passwd');
console.log(err.message);
It also supports chaining of errors while maintaining each error’s message:
var VError = require('verror');
var fs = require('fs');
var filename = '/nonexistent';
fs.stat(filename, function (err1) {
var err2 = new VError(err1, 'stat "%s"', filename);
console.error(err2.message);
});
You can get the next-level error using err.cause()
:
console.error(err2.cause().message);
Chaining of errors can be done as many times you want:
var err1 = new Error('No such file or directory');
var err2 = new VError(err1, 'failed to stat "%s"', '/junk');
var err3 = new VError(err2, 'request failed');
console.error(err3.message);
This will print: request failed: failed to stat "/junk": No such file or directory
The official documentation for Verror can be found in the readme of the repository: https://github.com/joyent/node-verror. The readme contains comprehensive information on how to use the library, constructors, properties and methods of the VError, WError, SError, and MultiError classes, as well as practical examples of how to use them.