error-ex
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
is-arrayish | 0.2.1 | 2.58 kB | MIT | prod |
Error-ex is a highly regarded Node.js package for easy error subclassing and stack customization. This npm package offers a convenient way to subclass and create unique Error types in a Node.js project enabling them to define more specific and user-friendly error information. Thus, enhancing the debugging process.
To incorporate error-ex into your Node.js project, first, you need to include it in your project file.
var errorEx = require('error-ex');
Creating a specific error message type is also quite simple using error-ex:
var JSONError = errorEx('JSONError');
var err = new JSONError('error');
err.name; // This will output "JSONError"
throw err; // Will throw an error "JSONError: error"
You can also add a stack line to the error message:
var JSONError = errorEx('JSONError', {fileName: errorEx.line('in %s')});
var err = new JSONError('error');
err.fileName = '/a/b/c/foo.json';
throw err;
// This will throw an error displaying the stack line location in the file path: "in /a/b/c/foo.json"
Appending error messages can be achieved with the following snippet:
var JSONError = errorEx('JSONError', {fileName: errorEx.append('in %s')});
var err = new JSONError('error');
err.fileName = '/a/b/c/foo.json';
throw err;
// Will throw an error "JSONError: error in /a/b/c/foo.json"
The error-ex documentation and usage specifics are available within the README file in the project's GitHub repository (https://github.com/qix-/node-error-ex). In addition to the detailed code examples, it describes the full API, including helper functions like errorEx.line(str)
and errorEx.append(str)
for creating stack lines and appending to the error.message
string, respectively. The documentation provides a full breakdown of how to create new ErrorEx error types, including how to customize them with additional properties and methods.