npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anandsuresh/smart-error

v1.0.0

Published

A smart error implementation

Downloads

3

Readme

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