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

whoops

v4.1.7

Published

It makes simple throw qualified errors.

Downloads

430,790

Readme

whoops

Last version Coverage Status NPM Status

It makes simple throw qualified errors. Inspired in errno, create-error-class and fault.

Why

  • An easy way to create qualified errors.
  • Using the standard Error interface in browser and NodeJS.
  • Attach extra information, being flexible with whatever user case.

This library is a compromise to provide a clean API for use Error native class.

Install

npm install whoops --save

Basically it turns:

const error = Error('Something is wrong')
error.name = 'DAMNError'
throw error // => 'DAMNError: ENOFILE, Something is wrong'

Into a one line more productive declaration:

const whoops = require('whoops')
const userError = whoops('UserError')

throw userError('User not found') // => 'UserError: User not found'

Creating Qualified Errors

Call whoops to get a constructor function. Every time you call the constructor, you get an Error instance:

const whoops = require('whoops')
const myError = whoops()
throw myError()

Create domain specific errors providing a className as first argument:

const whoops = require('whoops')
const userError = whoops('userError')
throw userError()

The qualified error will be extends from Error:

const whoops = require('whoops')
const userError = whoops('userError')
const error = userError()
console.log(error instanceof Error); // => true

Attach extra information passing a props as second argument:

const whoops = require('whoops')
const userError = whoops('userError', {code: 'ENOVALID'})
const err = userError()
console.log(`My error code is ${err.code}`) // => My error code is ENOVALID

You can associate dynamic props as well:

const whoops = require('whoops')
const userError = whoops('userError', {
  code: 'ENOVALID',
  message: props => `User '${props.username}' not found`
})

const err = userError({username: 'kiko'})
console.log(err.message) // => User 'kiko' not found

Error Types

By default you will get Error instances calling whoops, but you can get different errors calling the properly method:

| Name | Method | |----------------|------------------| | Error | whoops | | TypeError | whoops.type | | RangeError | whoops.range | | EvalError | whoops.eval | | SyntaxError | whoops.syntax | | ReferenceError | whoops.reference | | URIError | whoops.uri |

Extra: Always throw/return an Error!

If you code implementation is

  • synchronous, throws Error. If you just return the Error nothings happens!.
  • asynchronous, returns Error in the first argument of the callback (or using promises).

About asynchronous code, is correct return a Object that is not a Error in the first argument of the callback to express unexpected behavior, but the Object doesn't have a type and definitely can't follow a error interface for determinate a special behavior:

callback('LOL something was wrong') // poor
callback({message: 'LOL something was wrong' } // poor, but better
callback(whoops('LOL, something was wrong') // BEST!

Passing always an Error you can can associated different type of error with different behavior:

switch (err.name) {
  case 'JSONError':
    console.log('your error logic here')
    break
  default:
    console.log('undefined code')
    break
};

Related

License

MIT © Kiko Beats