Create typed error classes with error codes, cause chains, and type guards
Install
npm install error-with-cause
Usage
import {createErrorClass, isErrorWithCode} from 'error-with-cause';
const NotFoundError = createErrorClass('NotFoundError', 'ERR_NOT_FOUND');
try {
throw new NotFoundError('User not found', {
data: {userId: 123},
cause: new Error('DB lookup failed'),
});
} catch (error) {
if (isErrorWithCode(error, 'ERR_NOT_FOUND')) {
console.log(error.code);
console.log(error.data);
console.log(error.toJSON());
}
}
API
createErrorClass(name, code, options?)
Returns a custom error class constructor.
name
Type: string
The error class name.
code
Type: string
The error code.
options
Type: object
parent
Type: ErrorConstructor
Default: Error
The parent error class to extend.
isErrorWithCode(error, code)
Type guard that returns true if error is an Error with a matching .code property.
Related
License
MIT