VError: Rich JavaScript Error Handling

1.10.1 · maintenance · verified Sun Apr 19

The `verror` library, currently at stable version 1.10.1, provides a set of robust error classes designed for creating richer, more debuggable JavaScript errors. Its core strength lies in implementing Joyent's best practices for error handling, offering features like `printf`-style message formatting for clear, human-readable errors, and sophisticated error chaining. `VError` allows chaining errors while preserving messages from all layers of the call stack, providing a comprehensive diagnostic trail. Conversely, `WError` wraps and chains errors but hides lower-level messages from the top-level error, making it suitable for public API endpoints where internal details should be obscured. The library also includes `SError` for stricter `printf` argument validation and `MultiError` for aggregating errors from parallel operations. While highly functional, its release cadence is slow, with the last major update being several years ago, and it primarily targets CommonJS environments, lacking native ESM support. This makes it a mature, stable choice, though potentially requiring adaptation for modern JavaScript module systems.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates VError's core features including printf-style messages, error chaining, accessing underlying causes, and attaching informational properties, using a file system operation as an example.

const VError = require('verror');
const fs = require('fs');
const filename = '/path/to/nonexistent_file_example'; // Ensure this file path does not exist for the example

fs.stat(filename, function (err1) {
    if (err1) {
        // Wrap the original filesystem error with specific context
        const err2 = new VError(err1, 'Failed to retrieve stats for file "%s"', filename);
        
        // Further wrap with a higher-level operational context
        const err3 = new VError(err2, 'Critical system operation failed during file access');

        console.error('Full nested error message:', err3.message);
        // Expected output: Critical system operation failed during file access: Failed to retrieve stats for file "/path/to/nonexistent_file_example": ENOENT, stat '/path/to/nonexistent_file_example'

        console.error('Immediate cause of err3:', err3.cause().message);
        // Expected output: Immediate cause of err3: Failed to retrieve stats for file "/path/to/nonexistent_file_example": ENOENT, stat '/path/to/nonexistent_file_example'

        console.error('Original underlying error:', err3.cause().cause().message);
        // Expected output: Original underlying error: ENOENT, stat '/path/to/nonexistent_file_example'

        // Example of adding custom informational properties
        const detailedError = new VError({
            cause: err3,
            name: 'FileOperationError',
            info: {
                requestedPath: filename,
                operationType: 'readStats',
                timestamp: new Date().toISOString()
            }
        }, 'Comprehensive error for logging');
        console.error('Error info properties:', detailedError.info);
    } else {
        console.log(`File ${filename} exists. This example requires a non-existent file.`);
    }
});

view raw JSON →