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

whoops

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

whoops

Simplification of Error Constructor.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
144K
increased by27.82%
Maintainers
1
Weekly downloads
 
Created
Source

Whoops

Last version Build Status Dependency status Dev Dependencies Status NPM Status Donate

Simplification of Error Constructor.

Why

  • An easy way to create qualified errors.
  • Using the standard Error interface in browser and NodeJS.
  • Attach extra information, depending of your case of use.

Basically turns:

var error = new Error('ENOFILE, Something is wrong');
error.name = 'DAMNError';
error.code = 'ENOFILE';

console.log(error.name) // => 'DAMNError: Something is wrong'
return error;

into more productive Error constructor:

var Whoops = require('Whoops');
var error = new Whoops('DAMError', 'ENOFILE', 'Something is wrong');

console.log(error.name) // => 'DAMNError: ENOFILE, Something is wrong'
return error;

Also support object constructor and possibility to define more fields:

var error = new Whoops({
  name: 'DAMError', , ''
  code: 'ENOFILE'
  message: Something is wrong
  path: 'filepath'
});

console.log(error.name) // => 'DAMNError: ENOFILE, Something is wrong'
console.log(error.path) // => 'filepath'
return error;

Install

npm install whoops --save

If you want to use in the browser (powered by Browserify):

bower install whoops --save

and later link in your HTML:

<script src="bower_components/whoops/dist/whoops.js"></script>

Usage

Load the constructor as a common NodeJS dependency:

var Whoops = require('whoops');

Now, the next time that you need an error you have two ways to create.

If you don't need to specify to many things associated with the error, you can create it inline mode. Just provide the error type and the description as string:

throw new Whoops('JSONError', 'The format of the JSON is invalid');

JSONError: The format of the JSON is invalid
  at new Whoops (/Users/josefranciscoverdugambin/Projects/whoops/lib/Whoops.coffee:6:17)
  at Object.<anonymous> (/Users/josefranciscoverdugambin/Projects/whoops/example.js:3:7)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:935:3

Additionaly you can provide the error code that will be associated and printed in the message:

throw new Whoops('JSONError', 'NotValidJSON', 'The format of the JSON is invalid');

JSONError: NotValidJSON, The format of the JSON is invalid
  at new Whoops (/Users/josefranciscoverdugambin/Projects/whoops/lib/Whoops.coffee:6:17)
  at Object.<anonymous> (/Users/josefranciscoverdugambin/Projects/whoops/example.js:3:7)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:935:3

If you need to associate whatever thing with the error, you can use the Object param format:

throw new Whoops({
  name: 'JSONError',
  code: 'NotValidJSON',
  message: 'The format of the JSON is invalid',
  errno: 127,
  foo: 'bar'
});

This prints the same as the inline mode, but you can store whatever thing (as errno or foo in this case) with the error.

Always throw an Error object

If you code implementation is synchronous, return Error object under unexpected behaviors.

If you code implementation is asynchronous, return Error object under unexpected behaviors as well!

It's correct returns a object in a callback to express a unexpected behavior, but the object doesn't have a type and definetly doesn't follow a error interface:

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

Now you 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;
};

License

MIT © Kiko Beats

Keywords

FAQs

Package last updated on 20 Nov 2015

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