New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

error-with-cause

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

error-with-cause

Create typed error classes with error codes, cause chains, and type guards

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source
error-with-cause — Create typed error classes with error codes, cause chains, and type guards

Create typed error classes with error codes, cause chains, and type guards

CI License: MIT npm Ask DeepWiki Socket Node 20+

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); // 'ERR_NOT_FOUND'
		console.log(error.data); // {userId: 123}
		console.log(error.toJSON()); // Serialized error object
	}
}

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.

  • error-serialize - Serialize errors to plain objects

License

MIT

Keywords

error

FAQs

Package last updated on 23 Aug 2026

Related posts