Socket
Socket
Sign inDemoInstall

make-error

Package Overview
Dependencies
0
Maintainers
3
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    make-error

Make your own error types!


Version published
Weekly downloads
21M
increased by0.36%
Maintainers
3
Install size
12.9 kB
Created
Weekly downloads
 

Package description

What is make-error?

The make-error package is a simple library for creating custom error types in JavaScript. It is useful for defining new error constructors that work similarly to native Error types, allowing for better error handling and debugging in applications.

What are make-error's main functionalities?

Creating custom error types

This feature allows developers to create their own error types that can be used throughout their codebase. The custom error types will inherit from the base Error class, making them compatible with standard error handling mechanisms.

const makeError = require('make-error');
const CustomError = makeError('CustomError');

throw new CustomError('This is a custom error message.');

Extending custom error types

Developers can also extend their custom error types to create a hierarchy of errors. This is useful for creating more specific error types that still share common behavior with their parent error types.

const makeError = require('make-error');
const BaseError = makeError('BaseError');
const SpecificError = makeError(BaseError);

throw new SpecificError('This is a more specific error message.');

Other packages similar to make-error

Readme

Source

make-error

Package Version Build Status PackagePhobia Latest Commit

Make your own error types!

Features

  • Compatible Node & browsers
  • instanceof support
  • error.name & error.stack support
  • compatible with CSP (i.e. no eval())

Installation

Node & Browserify/Webpack

Installation of the npm package:

> npm install --save make-error

Then require the package:

var makeError = require("make-error");

Browser

You can directly use the build provided at unpkg.com:

<script src="https://unpkg.com/make-error@1/dist/make-error.js"></script>

Usage

Basic named error

var CustomError = makeError("CustomError");

// Parameters are forwarded to the super class (here Error).
throw new CustomError("a message");

Advanced error class

function CustomError(customValue) {
  CustomError.super.call(this, "custom error message");

  this.customValue = customValue;
}
makeError(CustomError);

// Feel free to extend the prototype.
CustomError.prototype.myMethod = function CustomError$myMethod() {
  console.log("CustomError.myMethod (%s, %s)", this.code, this.message);
};

//-----

try {
  throw new CustomError(42);
} catch (error) {
  error.myMethod();
}

Specialized error

var SpecializedError = makeError("SpecializedError", CustomError);

throw new SpecializedError(42);

Inheritance

Best for ES2015+.

import { BaseError } from "make-error";

class CustomError extends BaseError {
  constructor() {
    super("custom error message");
  }
}

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet

Keywords

FAQs

Last updated on 19 Feb 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc