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

errorifier

v0.2.0

Published

Creates Error uniformly.

Downloads

38

Readme

Errorifier

Last version Dependency status Dev Dependencies Status NPM Status Donate

Adopting an uniform errors policy based in NodeJS errors schema.

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.

The native Error global object in JavaScript is broken. It is different depending of your browser of your environment.

I feel that standard NodeJS Errors are more powerful: this follow the same format. For example, if you try to read a file that doesn't exist you have the follow error:

fs.readFile('filename', function(err, data) {
  console.log(err);
  // { [Error: ENOENT, open 'filename']
  //   errno: 34,
  //   code: 'ENOENT',
  //   path: 'filename' }
});

The error have a code that is useful because it's part of the output message. If you try to print the error:

console.log(err.message);
// => ENOENT, open 'filename'

This library pretend extend NodeJS standard error for whatever error that you need to create.

Install

npm install errorifier --save

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

bower install errorifier --save

and later link in your HTML:

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

Usage

Load the constructor as a common NodeJS dependency:

var Errorifier = require('errorifier');

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 Errorifier('NotValidJSON, The format of the JSON is invalid');

This will print the error and the stack trace:

Error: NotValidJSON, The format of the JSON is invalid
  at new Errorifier (/Users/josefranciscoverdugambin/Projects/errorifier/lib/Errorifier.coffee:6:17)
  at Object.<anonymous> (/Users/josefranciscoverdugambin/Projects/errorifier/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 Errorifier({
  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 return 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 Errorifier('LOL, something was wrong') // BEST!

Now you can associated different type of error with different behavior.

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

License

MIT © Kiko Beats