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

anomalies

v0.1.3

Published

Generic error categorization micro-library.

Downloads

1

Readme

anomalies

npm version

This is a simple node.js library inspired by Cognitect's Clojure micro-library.

The idea here is to help you better categorize errors as they occur, and better report these errors as actionable things to callers of your APIs. Anomalies are simple objects that have at least a key category in it, and which has a string value of one of the following:

| category | retry | fix | | ---- | ---- | --- | | Unavailable | yes | make sure callee healthy | | Interrupted | yes | stop interrupting | | Incorrect | no | fix caller bug | | Forbidden | no | fix caller creds | | Unsupported | no | fix caller verb | | NotFound | no | fix caller noun | | Conflict | no | coordinate with callee | | Fault | no | fix callee bug | | Busy | yes | backoff and retry |

Anomaly Conversion

You can convert an anomaly to an object, which will flatten out data within the anomaly, add a retriable flag, and add a description for any custom reason included in the anomaly.

const anomalies = require('anomalies');
let obj = anomalies.toObject({category: 'Busy'});
// returns: { category: "Busy", retriable: true }

More useful may be converting an anomaly to an HTTP response, which will choose a HTTP status code appropriate for the category, and will encode the object version of the anomaly as JSON and return that as the body of the response. The responses here are usable for things like AWS API Gateway, but may be usable for other systems (and, you can change the returned object however you wish).

const anomalies = require('anomalies');
let response = anomalies.toResponse({category: 'Forbidden'});
// returns {statusCode: 403, body: '{"category":"Forbidden","retriable":false}'}

You can pass any object to toObject and toResponse. If the value you pass is not an anomaly, then it treats it as if you had passed in {category: 'Fault'}.

Custom Reasons

You can register a "reason description" for custom reason codes with this library:

const anomalies = require('anomalies');
anomalies.registerReason('MY_CUSTOM_REASON', 'My custom thing failed');

Then if you include a reason field in an anomaly, converting that to an object will include your description and reason:

let obj = anomalies.toObject({category: 'Fault', reason: 'MY_CUSTOM_REASON'});
// returns {category: 'Fault', reason: 'MY_CUSTOM_REASON', error: 'My custom thing failed'}

API

  • isRetriable: tells if the argument passed in is a retriable error or not.
    • Input: any value.
    • Returns: boolean.
  • isCategory: tells if the argument passed in is a known anomaly category (a string).
    • Input: any value.
    • Returns: boolean.
  • isAnomaly: tells if the argument passed in is an anomaly.
    • Input: any value.
    • Returns: boolean.
  • toObject: flattens an anomaly, and populates reason and error fields if appropriate.
    • Input: an anomaly.
    • Returns: an object describing the anomaly.
  • toResponse: return an HTTP style response object for an anomaly.
    • Input: any object (expects an anomaly, however).
    • Returns: an object with statusCode and body set appropriately for the argument.
  • registerReason: register a custom error code with a human-readable description.
    • Inputs:
      • Your custom error code (usually a string, but may be whatever you like).
      • A description string of your custom error code.
  • toAnomaly: ensure a value is an anomaly.
    • Input: any value.
    • Output: an anomaly; if already an anomaly, returns the input. If an instance of Error, returns an anomaly with category Fault and message set to the message field of the error. Otherwise, returns an anomaly with category Fault.

Immutable Values

This library can optionally support immutable data structures, by passing in a immutable Map to isAnomaly or isRetriable, and you can get an immutable value from toObject or toResponse if you pass a second argument asImmutable = true.