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

node-exceptions

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-exceptions

personalized and extendable exception class for nodejs

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
decreased by-11.31%
Maintainers
1
Weekly downloads
 
Created
Source

Node Exceptions

Version Build Status Coverage Status License

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
}

Also NE comes with some commonly required Exception classes which includes.

DomainException

When something excepted has failed. For example image upload mismatch extension.

throw new NE.DomainException()
InvalidArgumentException

Method arguments are invalid or incomplete.

throw new NE.InvalidArgumentException()
RangeException

Error caused due to arithmetic operations.

throw new NE.RangeException()
RuntimeException

An error occured after all required checks.

throw new NE.RuntimeException()
HttpException

Http specific errors.

throw new NE.HttpException()

Keywords

FAQs

Package last updated on 08 Aug 2016

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