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

error-accumulator

v0.1.0

Published

An accumulator for only errors that tries to interrupt and throw an error if the storage is not empty.

Downloads

2

Readme

error-accumulator

An accumulator for only errors that tries to interrupt and throw an error if the storage is not empty.

An instance of Error or this accumulator can be add to this accumulator but any other object can not be. Therefore, the accumulator works well with Short-circuit evaluation.

This module provides the class ErrorAccumulator which has an internal array. And the method add push an input object to the array if the input is a instance of Error or ErrorAccmulator. The return value of add is always ErrrorAccmulator itself. The method try will throw an Error if the internal array is NOT empty. If the array is empty, try returns ErrorAccmulator itself.

Example

const ErrorAccumulator = require('error-accumulator');

class UserError extends Error {
  constructor(message) {
    super(message);
    this.name = 'UserError';
  }
}
const a = { b: {} };

const acc = new ErrorAccumulator();
try {
  acc
    .add(true     || new Error('not add'))
    .add(-1       || new Error('not add'))
    .add('string' || new Error('not add'))
    .add([]       || new Error('not add'))
    .add({}       || new Error('not add'))
    .add(a        || new Error('not add'))
    .add(a.b      || new Error('not add'))
    .try() // returns `acc`
    .add(false             || new Error('false'))
    .add(null || undefined || new TypeError('null or undefined'))
    .add(NaN || 0          || new EvalError('NaN or 0'))
    .add('' || "" || ``    || new SyntaxError('empty'))
    .add(a.b.c             || new UserError('Cannot read property \'c\''))
    .try(); // throws an error
} catch (e) {
  // `e` is a instace of `Error` that `ErrorAccmulator` creates.

  console.log(e.name);
  // a string serializing an array of all the error names:
  // ["Error","TypeError","EvalError","SyntaxError","UserError"]

  console.log(e.message);
  // a string serializing an array of all the error messages:
  // ["false","null or undefined","NaN or 0","empty","Cannot read property 'c'"]

  console.log(e.stack);
  // a concatenation of all the error stacks:
  // Error: false
  //     at Object.<anonymous> (/path/to/test/index.test.js:22:35)
  //     ...
  // TypeError: null or undefined
  //     at Object.<anonymous> (/path/to/test/index.test.js:23:35)
  //     ...
  // EvalError: NaN or 0
  //     at Object.<anonymous> (/path/to/test/index.test.js:24:35)
  //     ...
}

console.log(acc.has()); // true
console.log(acc.error()); // returns 'e'
console.log(acc.errors());
// returns an array of all the inputs pushed by `add()`:
// [
//   Error('false'),
//   TypeError('null or undefined'),
//   EvalError('NaN or 0'),
//   ...
//   UserError('Cannot read property \'c\'')
// ]

// add another accumulator to the accumulator:
const empty = new ErrorAccumulator();
const another = (new ErrorAccumulator())
  .add(new Error('0'))
  .add(new Error('1'))
  .add(new Error('2'));

acc
  .add(empty) // nothing to be added
  .add(another);
console.log(acc.error().message);
// ["false", "null or ... property 'c'","0","1","2"]

Methods

.add(object)

The add() method always return the accumulator instance. If an input object is an instance of

  • Error or
  • ErrorAccumulator,

add() pushes the object to the internal array.

class UserError extends Error {
  constructor(message) {
    super(message);
    this.name = 'UserError';
  }
}

try {
 (new ErrorAccumulator())
  .add(new Error('built-in Error'))
  .add(new EvalError('built-in EvalError'))
  .add(new RangeError('built-in RangeError'))
  .add(new ReferenceError('built-in ReferenceError'))
  .add(new SyntaxError('built-in SyntaxError'))
  .add(new TypeError('built-in TypeError'))
  .add(new UserError('This is a UserError'))
  .try();
} catch (e) {
  console.error(e.name);
  // ["Error","EvalError",...,"UserError"]
  console.error(e.message);
  // ["built-in Error","built-in EvalError",...,"This is a UserError"]
}

If a type of an input is ErrorAccmulator, add() flats the all the errors of the input and push all the elements to the internal array individually.

const a = new ErrorAccumulator();
const b = new ErrorAccumulator();
a.add(new Error('A1'));
b.add(new Error('B1'));
a.add(new Error('A2'));
b.add(new Error('B2'));

a.add(b);
console.error(a.error().message);
// ["A1","A2","B1","B2"]

.try()

The try() method attempts to throw an error. If the internal array is NOT empty, try() throws an error which ErrorAccumulator creates (See .error()). If the internal array is empty, try() returns the accumulator instance.

const acc = new ErrorAccumulator();
try {
  acc
    .add(true)
    .add(false)
    .try() // not thrown
    .add(null)
    .add(undefined)
    .try() // not thrown
    .add(0)
    .add(NaN)
    .add(-1)
    .try() // not thrown
    .add('')
    .add('not empty string')
    .try() // not thrown
    .add({})
    .add([])
    .add(function func() {})
    .add(() => {})
    .try() // not thrown
    .add(/RegExp/)
    .add(Symbol('symbol'))
    .add(new Blob('blb', { type: 'blob/error' }))
    .add(new Date())
    .try(); // not thrown
} catch (e) {
  // never reach here
  console.error(e.message);
}
console.log(acc.has()); // false
console.log(acc.error()); // null
console.log(acc.errors()); // []

.has()

The method has() returns a boolean indicating whether the internal array is empty or not.

.error()

The method error() returns an Error instance if the internal array is not empty. If the internal array is empty, error() returns null.

An instance which error() returns has the standard properties of Error such that:

  • .name: a string indicating an array of all the error names by JSON.stringify.
  • .message: a string indicating an array of all the error messages by JSON.stringify.
  • .stack: a string concatinating all the error stack.

.errors()

The method errors() return an array of all the inputs pushed by add(). Note that if an instance of ErrorAccumulator has been added, the elements of the instance is flatten. If the internal array is empty, errors() returns a empty array [].

const a = new ErrorAccumulator();
console.log(a.errors()); // []
a.add(new Error());
a.add(new EvalError());

const b = new ErrorAccumulator();
b.add(new RangeError());
b.add(new ReferenceError());

const errors = a.add(b).errors();

console.log(errors[0] instanceof Error); // true
console.log(errors[1] instanceof EvalError); // true
console.log(errors[2] instanceof RangeError); // true
console.log(errors[3] instanceof ReferenceError); // true

License

MIT