boo-boo
v1.0.0-alpha.10
Published
Opinionated better errors for Node.js, browsers and React Native.
Downloads
6
Maintainers
Readme
boo-boo ·
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 ofError
, itsmessage
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. Seenames
for all available names.message
(String): Optional. Human-readable description of the error.stack
(String): Stack trace.isBoo
(Boolean): A readonly property that always returnstrue
to simplify error instance checking ofBoo
. It's intended to replace a somewhat ugly and counter-intuitiveerr instanceof boo.Internal
with a much more slickerr.isBoo
.
Methods
toString()
→ String: Overrides the defaultError#toString()
method in order to provide additional data.toJSON()
→ Object: A plain object representation that is required forJSON.stringify()
. The resulting object containsname
,message
andstack
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