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

failure

v1.1.1

Published

Easily generate "custom" error objects with addition properties which can be stringfied with JSON.stringify

Downloads

32,641

Readme

failure

Made by unshiftVersion npmBuild StatusDependenciesCoverage StatusIRC channel

Failure is a small helper library which allows you to easily generate custom error objects which can hold addition properties which could be helpful for debugging your application. In addition to that, it automatically adds a missing toJSON function to the Error object so you can actually get the message and stack trace once you JSON.stringify the error instance.

Installation

The module is written with browsers and servers in mind and should run in any environment that runs ES3. The module it self is released in the public npm registry and can be installed using:

npm install --save failure

The --save flag tells npm to automatically add the installed version to your package.json file as new dependency.

Usage

First of all, start with including this module in your code:

'use strict';

var failure = require('failure');

Now every time you want to pass or create a new Error instance, you can use the failure function to generate the error for you. The failure method accepts 2 arguments:

  1. An Error instance that just needs extra props, or a string that should be transformed to an Error. Please do note that when using a string you will have an extra trace in your stack trace as the stack trace will be made inside the failure function instead of where you called the failure function.
  2. An object with extra properties that should be introduced on the supplied or generated Error instance. These properties will not override existing properties on the Error instance.

Before the function returns the generated Error instance it checks if it also needs to add the missing .toJSON method.

Below is a small usage example on how you could use this to provide extra information when things start failing when you make an HTTP request somewhere. If request something with an incorrect status code, you might want to know what statusCode was received, so we can easily add that to the Error object. Same as parse errors for JSON, you probably want to know what you received and failed.

request('https://googlllll.com', function (err, res, body) {
  if (err) return next(err);
  if (res.statusCode !== 200) return next(failure('Invalid statusCode'), {
    statusCode: res.statusCode
  });

  try { body = JSON.parse(body); }
  catch (e) {
    return next(failure(e, { 
      body: body 
    }));
  }

  next(undefined, body);
})

License

MIT