Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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!
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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.