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

@studio/fail

v1.8.0

Published

Fail with an Error object and a conventional code property

Downloads

292

Readme

Usage (async / await)

const { failure, INVALID } = require('@studio/fail');

function read(filename, callback) {
  if (!filename) {
    // Easily fail with a conventional error:
    throw failure('Missing filename', INVALID);
  }

  // ...
}

Usage (callback)

const fs = require('fs');
const { fail, then, INVALID } = require('@studio/fail');

function read(filename, callback) {
  if (!filename) {
    // Easily fail with a conventional error:
    fail(callback, 'Missing filename', INVALID);
    return;
  }

  // Wrap callbacks with and error handling, guarding from multiple invocations:
  fs.readFile(
    filename,
    'utf8',
    then(callback, (content) => {
      // Non-undefined return value is passed to callback:
      return content.trim();
    })
  );
}

Conventions

Errors should always have a code property with an uppercase error code. To simplify error handling, use the provided fail utility function and code constants.

Error codes follow these conventions:

  • Fatal errors should have an error code starting with E_. This is the asynchronous equivalent to throw. The provided message is not supposed to be shown to the user.
  • Other error codes have no prefix and are not considered fatal, for example a validation error. The provided message may be shown to the user.
  • If no code property is provided, it defaults to E_FAILED.

The provided error codes can be handled generically. You may define additional error codes as needed.

Install

❯ npm i @studio/fail

API

  • failure(message[, cause][, code[, properties]]): Create an Error with the given message and cause and code and properties. If no code is provided it defaults to E_FAILED. The cause must be an error object.
  • fail(callback, message[, cause][, code[, properties]]): Creates a failure and invoked the given callback with it.
  • isFatal(error): Whether the given error has a code property the starts with E_ or has no code property.
  • then(callback, next): Create a callback function that invokes callback(err) if an error occurred and next(result) on success. Throws if the function is invoked more than once with error code E_FAILED and err as the cause. If next returns a non-undefined value, the callback is invoked with that result.

Error codes

  • E_FAILED: Fatal error.
  • INVALID: Invalid or missing argument or parameter.
  • FORBIDDEN: User is not allowed to access.
  • NOT_FOUND: Resource does not exist.

Examples

Throwing errors:

const { failure, INVALID } = require('@studio/fail');

// Fail with a message:
throw failure('Oups!');

// The previous is the same as this:
throw failure('Oups!', E_FAILED);

// Fail with `code` INVALID:
throw failure('Oups!', INVALID);

// Fail with a `cause`:
const cause = new Error();
throw failure('Oups!', cause);

// Fail with a `cause` and `code` INVALID:
throw failure('Oups!', cause, INVALID);

// Fail with `properties` and `code` INVALID:
throw failure('Oups!', INVALID, { some: 42 });

Invoking callbacks with errors:

const { fail, FORBIDDEN } = require('@studio/fail');

fail(callback, 'Oups!', FORBIDDEN);

Related modules

  • 👻 Studio Log is a tiny ndjson logger that is code and cause aware.
  • 📦 Studio Changes is used to create the changelog for this module.

License

MIT