What is verror?
The verror npm package provides utilities for constructing and formatting errors in Node.js applications. It allows for richer error information, chaining of errors, and better error handling mechanisms. This package is particularly useful for applications that require detailed error tracking and reporting.
What are verror's main functionalities?
Creating rich errors
This feature allows developers to create errors that can encapsulate more information than standard Error objects. It is useful for debugging and error tracking.
const VError = require('verror');
const err = new VError('This is a rich error with a cause.');
Chaining errors
Chaining errors enables developers to trace through a sequence of errors that led to a failure, making it easier to understand the context and root cause of an issue.
const VError = require('verror');
const cause = new Error('cause');
const err = new VError(cause, 'A higher-level error.');
Formatting error messages
This feature allows for the inclusion of additional information within error objects and provides a structured way to access this information. It's particularly useful for logging and reporting errors with specific codes or data.
const VError = require('verror');
const err = new VError({
name: 'MyError',
info: { errorCode: 'E123' }
}, 'An error occurred.');
const info = VError.info(err);
console.log(info.errorCode); // Outputs: E123
Other packages similar to verror
winston
Winston is a multi-transport async logging library for Node.js. While primarily focused on logging rather than error handling, it shares the goal of providing rich information for debugging purposes. Unlike verror, winston is more about logging levels and transports rather than constructing and chaining errors.
pino
Pino is a very low overhead Node.js logger, which, like winston, focuses on logging rather than error handling. It provides mechanisms for structured logging and performance. Pino and verror serve different aspects of application diagnostics; pino does not offer features for error object enhancement or chaining.
verror: richer JavaScript errors
This module provides VError, which is like JavaScript's built-in Error class,
but supports a "cause" argument and a printf-style message. The cause argument
can be null. For example:
if (err)
throw (new VError(err, 'operation "%s" failed', opname));
If err.message is "file not found" and "opname" is "rm", then the thrown
exception's toString() would return:
operation "rm" failed: file not found
This is useful for annotating exceptions up the stack, rather than getting an
extremely low-level error (like "file not found") for a potentially much higher
level operation.
Additionally, when printed using node-extsprintf using %r, each exception's
stack is printed.
Example
First, install it:
# npm install verror
Now, use it:
var mod_fs = require('fs');
var mod_extsprintf = require('extsprintf');
var mod_verror = require('../lib/verror');
mod_fs.stat('/nonexistent', function (err) {
console.log(mod_extsprintf.sprintf('%r',
new mod_verror.VError(err, 'operation failed')));
});
outputs:
EXCEPTION: VError: operation failed: ENOENT, no such file or directory '/nonexistent'
at Object.oncomplete (/home/dap/node-verror/examples/simple.js:7:6)
Caused by: EXCEPTION: Error: Error: ENOENT, no such file or directory '/nonexistent'