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

rejection-js

v1.0.3

Published

A module to create better error objects

Downloads

9

Readme

rejection-js

A module which contains better error objects.

npm version Build Status Coverage Status

###Why does this module exist?

  • I wanted to capture an error and then wrap it in another to better show what was going on through the stack.
  • I wanted a clean output to the console.

###Install

npm install rejection-js

###Usage Here is a simple example.

const RejectionJs = require('rejection-js');
const formater = RejectionJs.ConsoleFormatter();

const message = 'foo';
const data = { bar: 'baz' };
const innerRejection = null;

try {
    throw new RejectionJs.Rejection(message, data, innerRejection);
} catch (rej) {
    console.log(formater.format(rej));
}

##Rejections

###Rejection

The rejection object is the base object for this module. It inherits from Error. There are many different ways to create an instance of this object. It takes three paramaters, all of which are required but nilable.

const RejectionJs = require('rejection-js');

RejectionJs.Rejection( issue | null | undefined,
          data | null | undefined,
          innerRejection | null | undefined );
  • issue: This property can take two types as values.
    • string: If a string is provided then the message property of the new rejection object will be set to this value.
    • Error: If an object of type Error is passed then the value of the message property of the passed Error instance will become the value of the message property of the new rejection object. Also the stack value of the Error object is used as well.
  • data: This property can be any value. It's main purpose is to allow additional data to be defined for the rejection object.
  • innerRejection: This can be any value, but the type passed will have different results.
    • Error: If an Error type is passed then a new Rejection object will be created using the passed Error object.
    • Rejection: If the object passed is an instance of Rejection then this object will be used without modification.
    • other: If any other type is passed then a new Rejection object will be created where the issue paramater is null and then data paramater gets the passed value.

Each instance of a rejection object not only provides the properties message, data, and innerRejection, it also has a stack propery. This property is an array of the current call stack. The size of the array can be affected by the value of Error.stackTraceLimit.

###HttpRejection

The HttpRejection object can be accessed the same way as Rejection and used exactly the same. The only difference is an additional propery has been added to allow the user to define the http code.

const RejectionJs = require('rejection-js');

RejectionJs.HttpRejection( issue | null | undefined,
          code | null | undefined,
          data | null | undefined,
          innerRejection | null | undefined );
  • code: This property can be any value. It's main purpose is to allow the used to define an http code with the rejections.

Inheriting from Rejection

If you want to inherit from Rejection its real easy. This is the format I use.

import Util from 'util';

function MyRejection(issue, myProperty, data, innerRejection) {
    if (!(this instanceof MyRejection)) {
        return new HttpRejection(issue, myProperty, data, innerRejection);
    }

    Rejection.inherit(this, issue, myProperty, data, innerRejection, {
        MyProperty: 'myProperty'
    });

    this.myProperty = myProperty;
}

Util.inherits(MyRejection, Rejection);

return MyRejection;

The main part to review here is "Rejection.inherit". This function replaces "Rejection.call". The reason is the last parameter pased is an object which defines additional properties which will are avaliable on the object. At the moment this is mainly used by the formatters so they know that these properties are are to be formated as output. The format of this property is

const additionalProperties = {
    label: propertyName
};

##Formatters Formatters will be used to convert an instance of rejection to a string for output to some log, file, or console.

###ConsoleFormatter The console formatter is built to format a rejection object into a string for output to the console. Of course the string can be saved anywhere if desired.

const RejectionJs = require('rejection-js');

const options = null;

const formater = RejectionJs.ConsoleFormatter(options);

const rej = new RejectionJs.Rejection();

console.log(formater.format(rej));

This formatter can take options which can change the format of the result.

  • stackTraceLimit: Although the size of the stack property in a Rejection object can be affected by Error.stackTraceLimit, the stackTraceLimit can limit the number outputed to the formated string. The default value is Infinity.
  • padding: This the number of spaces which is added to the front of every line so the output lines up. The default is 0.
  • innerPaddingIncrement: This is the number of spaces added to the padding everytime an innerRejection is written to the output. The default is 5.
  • showNil: Setting this to true will add any items with undefined or null values to the output. By default this is true.
  • useColors: This will enable the console colors. The default is false.
  • colors: These are the colors which will be used when outputting. The colors of data properties are affected by the values set by "util.inspect.styles".
    • label: This is color to use when writing the labels of each property. The default is "grey".
    • stack: This is color to use when writing the stack. The default is "cyan".
    • message: This is color to use when writing the message. The default is "red".
  • data: This property has the same properties and default values as Util.inspect, which is used to output the value of the data property and any additional properties. The useColors property overides the color property so this does not need to be defined.