1.0.0-alpha.10 • Published 7 years ago

boo-boo v1.0.0-alpha.10

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

boo-boo · npm

Opinionated better errors for Node.js, browsers and React Native.

Working on a project I sooner or later come to a point where I would like to have my own errors, that can be easily distinguished from all other errors as well as from each other. Another quite important need is to send error messages and stack traces as JSON responses, but since native errors are missing toJSON() method they can't be properly stringified.

Table of Contents

Install

$ yarn add boo-boo

or

$ npm install --save boo-boo 

Usage

import boo from 'boo-boo';
// or
const boo = require('boo-boo');

// ...

try {
    JSON.parse(someJsonString);
}
catch (err) {
    throw new boo.Validation(err);
}

API

new boo.[name]([messageOrError])

A Boo constructor, inherited from Error. Each variant creates an instance of Boo with a specific name property. For a list of all available names see names.

Arguments

  • [messageOrError] (any|Error): Optional. Error description that will be coerced to a string. If the value is an instance of Error, its message property will be taken instead.

Examples

const err = new boo.Internal('boo!');
console.log(err.message); // 'boo!'

const err = new boo.Request(new TypeError('boo!'));
console.log(err.message); // 'boo!'

Boo

An instance of Boo created by one of constructors above.

Properties

  • name (String): Error name that is set upon creation. See names for all available names.
  • message (String): Optional. Human-readable description of the error.
  • stack (String): Stack trace.
  • isBoo (Boolean): A readonly property that always returns true to simplify error instance checking of Boo. It's intended to replace a somewhat ugly and counter-intuitive err instanceof boo.Internal with a much more slick err.isBoo.

Methods

  • toString()String: Overrides the default Error#toString() method in order to provide additional data.
  • toJSON()Object: A plain object representation that is required for JSON.stringify(). The resulting object contains name, message and stack properties (if exist).

names

A plain object of names used by Boo constructors:

  • Database
  • External
  • Internal
  • Request
  • Timeout
  • Validation

This list can be supplemented, PRs are welcome.

Examples

const err = new boo.Internal();
console.log(err.name === boo.names.Internal); // true