bugsy
Helper to deal with errors lifecycle in javascript.
Dealing with errors is a common problem in every small or large project. While a lot of libraries simply focus on their creation, this library is meant to deal with their lifecycle.
Features
- Universal module
- Error severity
- Custom metadata
- Flowtype
Installation
With NPM:
$ npm install bugsy
With Yarn:
$ yarn add bugsy
Usage
import * as bugsy from 'bugsy';
const RAN_AWAY = 'ran_away';
const THROW_MISSED = 'throw_missed';
const ranAway = bugsy.createError(RAN_AWAY, 'It ran away, again');
const throwMissed = bugsy.createError(THROW_MISSED, 'Throw totally missed');
function capture() {
const r = Math.random();
if (r < 0.3) {
throw throwMissed({ severity: bugsy.syslog.WARNING });
} else if (r < 0.6) {
throw ranAway();
} else {
throw new Error();
}
}
function handler(fn) {
try {
fn();
} catch (err) {
console.log(bugsy.toString(err));
}
}
handler(() => {
try {
capture();
} catch (err) {
switch (err.code) {
case THROW_MISSED:
console.log('Oh well...');
break;
case RAN_AWAY:
throw err.setSeverity(bugsy.syslog.CRITICAL).addMeta({ firstTry: true });
default:
throw bugsy.convert(err).setSeverity(bugsy.syslog.EMERGENCY);
}
}
});
Licences
njakob/bugsy
is licensed under the MIT License.