
Company News
Jerod Santo Joins Socket as Head of Media
Allow myself to introduce... myself.
error-with-cause
Advanced tools
Create typed error classes with error codes, cause chains, and type guards
Create typed error classes with error codes, cause chains, and type guards
npm install error-with-cause
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); // 'ERR_NOT_FOUND'
console.log(error.data); // {userId: 123}
console.log(error.toJSON()); // Serialized error object
}
}
Returns a custom error class constructor.
Type: string
The error class name.
Type: string
The error code.
Type: object
Type: ErrorConstructor
Default: Error
The parent error class to extend.
Type guard that returns true if error is an Error with a matching .code property.
MIT
FAQs
Create typed error classes with error codes, cause chains, and type guards
The npm package error-with-cause receives a total of 8 weekly downloads. As such, error-with-cause popularity was classified as not popular.
We found that error-with-cause demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Company News
Allow myself to introduce... myself.

Research
/Security News
A Twitch browser extension on Chrome and Firefox forwards users’ live OAuth session tokens through proxies controlled by a Russian bot service.

Security News
Anthropic found biased reasoning and recklessness drove Claude Mythos 5 to publish malware on PyPI and compromise a security vendor.