What is err-code?
The 'err-code' npm package is a utility for creating error objects with custom error codes and properties. It simplifies the process of adding additional context to errors, making them more informative and easier to handle.
What are err-code's main functionalities?
Creating an error with a custom code
This feature allows you to create an error object with a custom error code. The custom code can be used to identify the error type more easily.
const createError = require('err-code');
const error = createError(new Error('Something went wrong'), 'E_CUSTOM');
console.log(error.code); // 'E_CUSTOM'
Adding custom properties to an error
This feature allows you to add custom properties to the error object, providing additional context and information about the error.
const createError = require('err-code');
const error = createError(new Error('Something went wrong'), 'E_CUSTOM', { detail: 'Additional error details' });
console.log(error.detail); // 'Additional error details'
Handling errors with custom codes
This feature demonstrates how to handle errors based on their custom codes, allowing for more specific error handling logic.
const createError = require('err-code');
try {
throw createError(new Error('Something went wrong'), 'E_CUSTOM');
} catch (error) {
if (error.code === 'E_CUSTOM') {
console.error('Custom error occurred:', error.message);
}
}
Other packages similar to err-code
create-error
The 'create-error' package provides a way to create custom error types with additional properties. It is similar to 'err-code' but focuses more on creating new error types rather than adding codes to existing errors.
custom-error-generator
The 'custom-error-generator' package allows you to generate custom error classes with additional properties. It is similar to 'err-code' but offers more flexibility in defining custom error classes.
verror
The 'verror' package is used for creating and manipulating chained errors. It provides more advanced features for error handling and is more complex compared to 'err-code'.
err-code
Create new error instances with a code and additional properties.
Installation
$ npm install err-code
- NPM
$ bower install err-code
- bower
The browser file is named index.umd.js which supports CommonJS, AMD and globals (errCode).
Why
I find myself doing this repeatedly:
var err = new Error('My message');
err.code = 'ESOMECODE';
err.detail = 'Additional information about the error';
throw err;
Usage
Simple usage.
var errcode = require('err-code');
throw errcode(new Error('My message'), 'ESOMECODE');
throw errcode(new Error('My message'), 'ESOMECODE', { detail: 'Additional information about the error' });
throw errcode(new Error('My message'), { detail: 'Additional information about the error' });
throw errcode('My message', 'ESOMECODE');
throw errcode('My message', 'ESOMECODE', { detail: 'Additional information about the error' });
throw errcode('My message', { detail: 'Additional information about the error' });
Tests
$ npm test
License
Released under the MIT License.