Socket
Socket
Sign inDemoInstall

make-error-cause

Package Overview
Dependencies
1
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

make-error-cause


Version published
Maintainers
1
Install size
21.8 kB
Created

Package description

What is make-error-cause?

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.

What are make-error-cause's main functionalities?

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);
}

Other packages similar to make-error-cause

Readme

Source

Make Error Cause

NPM version NPM downloads Build status Test coverage

Make your own error types, with a cause!

Features

  • Compatible with Node and browsers
  • Works with instanceof
  • Use error.stack and error.name
  • Appended cause with toString
  • Extends make-error

Installation

npm install make-error-cause --save

Usage

See the usages from make-error. Things work the same here, except the base function has a second argument for the "cause". The cause will be printed when using toString, but can also be accessed manually.

const CustomError = makeErrorCause('CustomError')

const cause = new Error('boom!')
const error = new CustomError('something bad', cause)

error.toString() //=> "CustomError: something bad\nCaused by: boom!"
error.stack // Works!
error.cause.stack // Handy!

Attribution

Inspired by verror, and others, but made much lighter for browser bundling in tight places.

License

Apache 2.0

Keywords

FAQs

Last updated on 12 Dec 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc