New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

error-ninja

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

error-ninja

Allows us to pre-define error types and error messages in each module.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Error-Ninja

Error handling can be a pain, it's tempting to just pass strings to callbacks so it's easier to handle, but using a proper Error object is a better way and Error Ninja can help with this.

What's the Point?

  1. Outputs a human-readable error message in the console.
  2. Gives you an error ID so your code can identify the error.
  3. Allows you to include extra data in the error and output this in the console.

Define Your Error Messages

First, define a hash of error IDs and error messages.

var ErrorNinja = require('error-ninja').define({
  'invalid-blog-id':    'The given blog ID does not exist!',
  'attachment-too-big': 'You cannot upload an attachment that big!',
  'some-error':         'Woah, this code is buggy!'
});

Create and Throw Errors

Now you can create an error. You can throw this error just like any other.

var err = new ErrorNinja('invalid-blog-id');
throw err;

/*
  Outputs...
  Error [invalid-blog-id]: The given blog ID does not exist!
     at new ErrorNinja (/path/to/Error-Ninja/errorNinja.js:36:19)
     at Object.<anonymous> (/path/to/myScript.js:59:13)
     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:906:3
 */

Include Data in the Error

If you need to include some extra properties in the error you can specify the second parameter. By default this data will be output to the console if the error is thrown, and you'll also be able to access it when handling your error.

var err = new ErrorNinja('attachment-too-big', { fileSize: 17483, maxSize: 1024 });
throw err;

/*
  Outputs...
  Error [attachment-too-big]: You cannot upload an attachment that big!
  ----------
  {"fileSize":17483,"maxSize":1024}
  ----------
     at new ErrorNinja (/path/to/Error-Ninja/errorNinja.js:36:19)
     at Object.<anonymous> (/path/to/myScript.js:59:13)
     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:906:3
 */

Turn Off Data Console Output

To prevent the extra error data being output in the console you can do one of two things:

  1. Pass false as the 3rd argument when creating an error:
var err = new ErrorNinja('some-error', { abc: 'do-not-output' }, false);
  1. Or you can turn off data output for all errors created by this instance of Error Ninja:
var ErrorNinja = require('error-ninja').define({ ... }, { outputData: false });

Access Useful Properties in the Error

Apart from the usual error properties you can also access the following additional properties that should help with handling your errors.

var err = new ErrorNinja('attachment-too-big', { fileSize: 17483, maxSize: 1024 });

err.id;     // "attachment-too-big"
err.human;  // "You cannot upload an attachment that big!"
err.data;   // (mixed) e.g. { fileSize: 17483, maxSize: 1024 }
            // data defaults to {}

Keywords

FAQs

Package last updated on 10 Sep 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