パッケージの詳細

whoops

kikobeats288.6kMIT5.0.1

It makes simple throw qualified errors.

constructor, custom, error, simple

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.
  • Less than 50 lines (~500 bytes)

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

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.1 (2025-01-28)

Bug Fixes

5.0.0 (2025-01-28)

⚠ BREAKING CHANGES

  • simplify implementation (#46)

Features

4.1.8 (2025-01-27)

4.1.7 (2023-10-29)

4.1.6 (2023-10-24)

4.1.5 (2023-09-05)

4.1.4 (2023-04-14)

4.1.3 (2023-04-14)

Bug Fixes

  • extend-error: handle undefined error#stack (#42) (2dc0cd1)

4.1.2 (2022-07-20)

4.1.1 (2022-05-27)

Bug Fixes

4.1.0 (2019-12-15)

  • build: update dependencies (af7e392)
  • fix(package): update clean-stack to version 2.1.0 (3275e3c)
  • fix(package): update mimic-fn to version 2.1.0 (db95daa)
  • fix(package): update mimic-fn to version 3.0.0 (e1cc912)

4.0.2 (2019-03-08)

  • fix(package): update clean-stack to version 2.0.0 (24952ae)
  • fix(package): update mimic-fn to version 1.2.0 (719fc26)
  • fix(package): update mimic-fn to version 2.0.0 (d5c78a1)
  • Refactor (64f751b)
  • Update compositor.json (e26b0fd)
  • Update compositor.json (da98ea7)
  • Update README.md (0228c84)
  • docs(readme): add Greenkeeper badge (4e1a125)

4.0.1 (2017-08-08)

4.0.0 (2017-08-08)

3.1.1 (2017-07-18)

  • Update README.md (4561a8b)
  • docs(readme): add Greenkeeper badge (9b93e2c)
  • chore(package): update clean-stack to version 1.2.0 (b2cb039)
  • chore(package): update clean-stack to version 1.3.0 (12fb684)

3.1.0 (2016-12-19)

3.0.3 (2016-12-19)

3.0.2 (2016-09-13)

  • chore(package): update clean-stack to version 1.0.0 (ff0f4ce)
  • Update (ce66183)

3.0.1 (2016-08-01)

  • Avoid eval for setup function name (1ecde20)
  • Remove interface string façade (14c5932)
  • Remove unncessary check (6a4d469)
  • Rename extend-error → create-extend-error (fd3726e)
  • Setup Error name in constructor (4ac076d)

3.0.0 (2016-07-30)

2.2.0 (2016-07-30)

2.1.0 (2016-05-19)

2.0.1 (2016-02-07)

2.0.0 (2016-02-03)

1.1.1 (2016-01-28)

1.1.0 (2016-01-14)

1.0.1 (2015-11-23)

  • 1.0.1 releases (56e26f2)
  • Fix typos in README.md (e9063f2)
  • Merge branch 'master' of github.com:kikobeats/whoops (a363134)
  • Merge pull request #4 from jorrit/patch-1 (d9156f6)
  • rewrite to avoid new (23e586b)
  • update to upgrade bower.json (b18589d)

1.0.0 (2015-11-20)

0.2.0 (2015-10-01)

0.1.3 (2015-07-28)

0.1.2 (2015-07-28)

0.1.1 (2015-07-28)

0.1.0 (2015-07-26)