Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
make-error-cause
Advanced tools
The make-error-cause npm package is designed to simplify the creation of custom error classes in JavaScript, while also allowing you to maintain the original error cause. This is particularly useful for error handling in complex applications where you need to preserve the stack trace and context of the original error.
Creating Custom Error Classes
This feature allows you to create custom error classes that can encapsulate an original error. The custom error class extends from `makeErrorCause.BaseError`, which helps in maintaining the original error's stack trace and context.
const makeErrorCause = require('make-error-cause');
class MyCustomError extends makeErrorCause.BaseError {
constructor(message, cause) {
super(message, cause);
this.name = 'MyCustomError';
}
}
try {
throw new Error('Original error');
} catch (err) {
throw new MyCustomError('Custom error message', err);
}
Preserving Original Error Cause
This feature demonstrates how to preserve the original error cause when throwing a new custom error. This is useful for debugging and logging, as it provides a complete error stack trace.
const makeErrorCause = require('make-error-cause');
class DatabaseError extends makeErrorCause.BaseError {
constructor(message, cause) {
super(message, cause);
this.name = 'DatabaseError';
}
}
try {
throw new Error('Connection failed');
} catch (err) {
throw new DatabaseError('Database operation failed', err);
}
The `verror` package provides a way to create and manage nested errors in JavaScript. It allows you to add context to errors and maintain the original error stack. Compared to `make-error-cause`, `verror` offers more features for error wrapping and context management.
The `error-ex` package is used to create easily extensible error objects with additional properties. It allows you to add custom properties to errors and maintain the original error cause. While `error-ex` focuses on extensibility, `make-error-cause` is more focused on preserving the original error stack.
The `extendable-error` package provides a simple way to create custom error classes that extend the native Error class. It does not specifically focus on preserving the original error cause, making `make-error-cause` a better choice for scenarios where maintaining the original error context is crucial.
Make your own nested errors.
instanceof
inspect()
)fullStack(err)
make-error
npm install make-error-cause --save
import { BaseError, fullStack } from "make-error-cause";
class CustomError extends BaseError {
constructor(message, cause) {
super(message, cause);
}
}
const error = new Error("Boom!");
const customError = new CustomError("Another boom!", error);
console.log(customError); // Automatically prints full stack trace using `fullStack(this)`.
console.log(customError.cause); // Check causes via the `.cause` property.
console.log(customError instanceof Error); //=> true
Inspired by verror
, and others, but created lighter and without core dependencies for browser usage.
Other references:
Apache 2.0
FAQs
Make your own nested error types!
The npm package make-error-cause receives a total of 406,126 weekly downloads. As such, make-error-cause popularity was classified as popular.
We found that make-error-cause demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.