Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-exceptions

poppinss97.6kMIT4.0.1TypeScript support: included

Extendable error class for nodejs to extend native errors

exception, node-errors, custom-errors, Error

readme

Node Exceptions

NPM Version Build Status Appveyor Coveralls

Throwing errors in Javascript does not give much information about the error type as it is really hard to throw custom exceptions. Node Exceptions is a tiny wrapper which will let you extend the Error class and throw custom errors.

Why custom errors

Errors are thrown anywhere inside the code and handling them properly is required. For example you have an HTTP application, which can throw multiple errors and in order to handle those errors gracefully, you need to know the error types or their names.

switch (err.name) {
  case 'HttpException':
    // do something
  case 'RunTimeException':
    // do something else
}

Install

npm i --save node-exceptions

Creating custom errors

const NE = require('node-exceptions')

class MyCustomError extends NE.LogicalException {}

try {
  throw new MyCustomError('Something bad happened')
} catch (e) {
  console.log(e.status) // equals 500
  console.log(e.name) // equals MyCustomError
  console.log(e.message) // Something bad happened
  console.log(e.stack) // Error stack with correct reference to filepath and linenum
  console.log(e.toString()) // MyCustomError: Something bad happened
}

Custom error status

It is also possible to have a custom error status when throwing exceptions.

const NE = require('node-exceptions')

class HttpException extends NE.LogicalException {}

try {
  throw new HttpException('Page not found', 404)
} catch (e) {
  console.log(e.status) // equals 404
}

API Docs

Access complete API Docs here

changelog

4.0.1 (2018-10-20)

4.0.0 (2018-10-20)

Features

  • message: only show errSh link if node env is development (#3) (ceaf9c5), closes #2
  • rewrite: redo in typescript (c04e145)

BREAKING CHANGES

  • rewrite: the 4th argument (errShLink) passed to the constructor now no longer prefixes the err.sh domain and expects a complete HTTP url

3.0.0 (2018-01-15)

Features

  • err.sh: add support for err.sh (e72c264)

2.0.2 (2017-06-12)

2.0.1 (2017-04-02)

2.0.0 (2017-03-15)

1.0.3 (2016-08-08)

1.0.2 (2016-08-05)

Features

  • exception: add support for a custom unique code (f00007a)

1.0.1 (2016-02-05)

1.0.0 (2016-02-04)

Features