Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

error-class

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

error-class

Typed errors that closely emulate the native Error class.

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Error Class

Build Status npm Version License

This module provides typed errors that closely emulate the native Error class to a pedantic degree.

$ npm install error-class

Usage

The default export is a function that accepts two arguments, the name of the typed error, and an optional function which gets invoked in the context of the error instance with the error arguments after the error setup is completed.

import errorClass from 'error-class'

const SpecialError = errorClass('SpecialError', function () {
  if (arguments.length > 1) this.specialProperty = arguments[1]
})

const instance = new SpecialError('baz', { foo: 'bar' })
instance.message // 'baz'
instance.specialProperty // { foo: 'bar' }

Details

import errorClass from 'error-class'

const HumanError = errorClass('HumanError')

const hungryError = new HumanError(`I'm hungry!`)
hungryError.name // 'HumanError'
hungryError.message // `I'm hungry!`
hungryError.stack // Platform-specific error stack trace.
hungryError.hasOwnProperty('name') // false (!)
hungryError.hasOwnProperty('message') // true
hungryError.hasOwnProperty('stack') // true


// Just like native errors, it doesn't require using `new`.
const thirstyError = HumanError(`I'm thirsty!`)
Object.keys(thirstyError).length === 0 // True, all properties are non-enumerable.
Object.keys(Object.getPrototypeOf(instance)).length === 0 // Prototype non-enumerable.
thirstyError.constructor === HumanError // True.
thirstyError.constructor.name === 'HumanError' // True.
thirstyError instanceof Error // True, errors inherit from native `Error` class.
thirstyError instanceof HumanError // Also true, of course.

License

This software is licensed under the MIT License.

Keywords

FAQs

Package last updated on 28 Sep 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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc