You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

makeerror

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

makeerror

A library to make errors.


Version published
Weekly downloads
21M
increased by0.12%
Maintainers
1
Install size
10.4 kB
Created
Weekly downloads
 

Package description

What is makeerror?

The makeerror npm package provides a simple way to create custom error types in JavaScript. It allows developers to define new error types with specific properties and methods, enhancing error handling in applications by making it more structured and informative.

What are makeerror's main functionalities?

Creating custom error types

This feature allows developers to create new error types. The example shows how to define a custom error type named 'MyError' and create an instance of it with a specific message.

const makeError = require('makeerror');
const MyError = makeError('MyError');
const errorInstance = new MyError('Something went wrong!');
console.log(errorInstance.name); // 'MyError'
console.log(errorInstance.message); // 'Something went wrong!'

Custom error with additional properties

This feature demonstrates how to add additional properties to a custom error type. In the example, a 'statusCode' property is added to the 'MyError' type, which can be useful for HTTP error handling.

const makeError = require('makeerror');
const MyError = makeError('MyError', { statusCode: 404 });
const errorInstance = new MyError('Not found');
console.log(errorInstance.statusCode); // 404

Other packages similar to makeerror

Readme

Source

makeerror Build Status

A library to make errors.

Basics

Makes an Error constructor function with the signature below. All arguments are optional, and if the first argument is not a String, it will be assumed to be data:

function(message, data)

You'll typically do something like:

var makeError = require('makeerror')
var UnknownFileTypeError = makeError(
  'UnknownFileTypeError',
  'The specified type is not known.'
)
var er = UnknownFileTypeError()

er will have a prototype chain that ensures:

er instanceof UnknownFileTypeError
er instanceof Error

Templatized Error Messages

There is support for simple string substitutions like:

var makeError = require('makeerror')
var UnknownFileTypeError = makeError(
  'UnknownFileTypeError',
  'The specified type "{type}" is not known.'
)
var er = UnknownFileTypeError({ type: 'bmp' })

Now er.message or er.toString() will return 'The specified type "bmp" is not known.'.

Prototype Hierarchies

You can create simple hierarchies as well using the prototype chain:

var makeError = require('makeerror')
var ParentError = makeError('ParentError')
var ChildError = makeError(
  'ChildError',
  'The child error.',
  { proto: ParentError() }
)
var er = ChildError()

er will have a prototype chain that ensures:

er instanceof ChildError
er instanceof ParentError
er instanceof Error

FAQs

Package last updated on 23 Oct 2021

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
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc