All Projects → Kikobeats → whoops

Kikobeats / whoops

Licence: MIT License
It makes simple create qualified errors.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to whoops

custom-exception-middleware
Middleware to catch custom or accidental exceptions
Stars: ✭ 30 (+7.14%)
Mutual labels:  error-handling
progress-bar-log
A component to display a progress bar and last X logs at the same time.
Stars: ✭ 44 (+57.14%)
Mutual labels:  error-handling
dx
JavaScript without `try...catch`.
Stars: ✭ 26 (-7.14%)
Mutual labels:  error-handling
bugsnag
Report errors with Bugsnag 🐛
Stars: ✭ 37 (+32.14%)
Mutual labels:  error-handling
failure
Error management
Stars: ✭ 1,448 (+5071.43%)
Mutual labels:  error-handling
fejl
Error-making utility for Node apps.
Stars: ✭ 30 (+7.14%)
Mutual labels:  error-handling
babel-errors
Nicer error messages for Babel
Stars: ✭ 15 (-46.43%)
Mutual labels:  error-handling
rescue
🚒✨ Rescue: better errors through types (a more type directed MonadThrow/MonadCatch)
Stars: ✭ 18 (-35.71%)
Mutual labels:  error-handling
failure
An error handling package for Go.
Stars: ✭ 24 (-14.29%)
Mutual labels:  error-handling
sentry-testkit
A Sentry plugin to allow Sentry report interception and further inspection of the data being sent
Stars: ✭ 78 (+178.57%)
Mutual labels:  error-handling
result17
A rust like Result type for modern C++
Stars: ✭ 13 (-53.57%)
Mutual labels:  error-handling
react-error-guard
⚛️An overlay for displaying stack frames based on create-react-app/packages/react-error-overlay
Stars: ✭ 18 (-35.71%)
Mutual labels:  error-handling
merr
🔥 Minimal and good enough error handling library for Clojure/ClojureScript
Stars: ✭ 25 (-10.71%)
Mutual labels:  error-handling
static-404
⚡️ A WordPress plugin to quickly send a 404 for missing static files
Stars: ✭ 24 (-14.29%)
Mutual labels:  error-handling
belay
Robust error-handling for Kotlin and Android
Stars: ✭ 35 (+25%)
Mutual labels:  error-handling
retryx
Promise-based retry workflow library.
Stars: ✭ 21 (-25%)
Mutual labels:  error-handling
miette
Fancy upgrade to std::error::Error.
Stars: ✭ 945 (+3275%)
Mutual labels:  error-handling
apollo-error-converter
Global Apollo Server Error handling made easy. Remove verbose and repetitive resolver / data source Error handling. Automatic Error catching, logging, and conversion to ApolloErrors.
Stars: ✭ 16 (-42.86%)
Mutual labels:  error-handling
gommon
A collection of common util libraries for Go
Stars: ✭ 26 (-7.14%)
Mutual labels:  error-handling
nuxt-winston-log
Nuxt module for logging SSR errors using winston
Stars: ✭ 41 (+46.43%)
Mutual labels:  error-handling

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].