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

@anandsuresh/smart-error

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anandsuresh/smart-error

A smart error implementation

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

smart-error

node (scoped) npm (scoped) npm Travis license GitHub followers Twitter Follow

A SmartError is a wrapper around the JavaScript Error class providing a few additional fields:

  • code: A unique code identifying the error
  • metadata: Optional metadata useful for debugging (e.g. the arguments passed to the function that threw the error)
  • cause: An optional error that is being wrapped by the SmartError

usage

const {create} = require('@anandsuresh/smart-error')

const MyError = create('MyError', {
  Unexpected: 'An unexpected error occurred.',
  BadArguments: 'One or more arguments passed were invalid.',
  TimedOut: 'A timeout occurred waiting for the operation to complete.'
})


function isOddNumber(number) {
  if (typeof number !== 'number')
    throw MyError.BadArguments(number)

  return (number % 2 === 0)
}

try {
  console.log(`isOddNumber(3) = ${isOddNumber(3)}`)
} catch (e) {
  if (e.isBadArguments) {
    console.error(`The argument $(e.metadata) is not a number!`)
  } else {
    console.error(e.toDetailedString())
  }
}

api

create(errorName, errors)

create takes 2 arguments:

  • errorName: the name of the error. E.g. 'MyError'
  • errors: an object mapping error codes to error messages. E.g. {Unexpected: 'An unexpected error occurred'}

The create() function creates a static function for each error code and an getter function to check if that is the specific error wrapped by the SmartError. Consider the following example:

const {create} = require('@anandsuresh/smart-error')

const MyError = create('MyError', {
  Unexpected: 'An unexpected error occurred.',
  BadArguments: 'One or more arguments passed were invalid.',
  TimedOut: 'A timeout occurred waiting for the operation to complete.'
})

The newly created MyError class will inherit from the JavaScript Error class and have the following instance properties:

  • name: The name of the error, in this case 'MyError'
  • code: The unique code of the error (Unexpected, BadArguments, TimedOut)
  • message: A human-readable error message
  • metadata: Optional metadata associated with the error (e.g. the arguments passed to the function that threw the error)
  • cause: An optional error that is being wrapped by the SmartError
  • stack: The stack-trace generated by the error
  • isSmartError: Helper property that wraps an instanceof check to return whether or not the instance is a SmartError
  • isUnexpected: Helper property that returns true if the MyError instance has the code Unexpected
  • isBadArguments: Helper property that returns true if the MyError instance has the code BadArguments
  • isTimedOut: Helper property that returns true if the MyError instance has the code TimedOut

MyError will also receive the following set of class methods:

  • Unexpected(metadata, cause): Creates an returns an instance of MyError with the code property set to Unexpected
  • BadArguments(metadata, cause): Creates an returns an instance of MyError with the code property set to BadArguments
  • TimedOut(metadata, cause): Creates an returns an instance of MyError with the code property set to TimedOut

FAQs

Package last updated on 10 Apr 2018

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