Package detail

error-custom-class

ehmicky24.1kMIT10.0.1

Create custom error classes

nodejs, javascript, stacktrace, library

readme

Node Browsers TypeScript Codecov Minified size Mastodon Medium

Create custom error classes.

Features

Example

import errorCustomClass from 'error-custom-class'

const UserError = errorCustomClass('UserError')
const DatabaseError = errorCustomClass('DatabaseError')

try {
  throw new UserError('message')
} catch (error) {
  console.log(error.name) // 'UserError'
  console.log(error instanceof UserError) // true
}

Install

npm install error-custom-class

This package works in both Node.js >=18.18.0 and browsers.

This is an ES module. It must be loaded using an import or import() statement, not require(). If TypeScript is used, it must be configured to output ES modules, not CommonJS.

API

The API is designed for simplicity. For more customization, error-class-utils should be used instead.

errorCustomClass(errorName)

errorName string\ Return value: CustomError

Creates a custom error class.

Related projects

Support

For any question, don't hesitate to submit an issue on GitHub.

Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.

Contributing

This project was made with ❤️. The simplest way to give back is by starring and sharing it online.

If the documentation is unclear or has a typo, please click on the page's Edit button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!

changelog

10.0.1

Documentation

  • Improve documentation in README.md

10.0.0

Breaking changes

  • Minimal supported Node.js version is now 18.18.0

9.0.0

Breaking changes

  • Minimal supported Node.js version is now 16.17.0

8.4.0

Features

  • Improve tree-shaking support

8.3.0

Features

  • Add browser support

8.2.1

Bug fixes

  • Fix package.json

8.2.0

  • Switch to MIT license

8.1.0

Features

  • Improve TypeScript types

8.0.0

Features

  • Remove unused dependency

7.0.0

Breaking changes

6.1.0

Features

  • Error props are now deep merged

6.0.0

Breaking changes

  • The onCreate and ParentClass options have been removed. Please use error-class-utils instead.

5.0.0

Breaking changes

  • The onCreate and ParentClass positional parameters are now named parameters inside an options object.

4.1.0

Feature

  • Allow specifying the parent error class

4.0.0

Breaking changes

  • The project was renamed from error-type to error-custom-class

3.0.0

Breaking changes

  • Error properties are now set by default using new CustomError('message', { props: { ... } }) instead of new CustomError('message', { ... })

2.0.1

Bug fixes

  • Fix types

2.0.0

Breaking changes

  • Split ErrorType type into ErrorConstructor and ErrorInstance
  • Add a template parameter ErrorName to all types

1.3.1

Bug fixes

  • Fix main function's return value's type

1.3.0

Features

  • Reduce npm package size

1.2.0

Features

  • Do not allow errors named 'Warning' or 'UnhandledPromiseRejection'