README
js-mybad
My friendly error base class - for Node/JavaScript.
Introduction
One in general always needs a application/library specific error base class, but the native errors are very limited in what meta/debugging information they can hold at time they are raised. For better debugging and error reporting/inspection this error base class allows to attach some additonal error context information that can be used to better understand the issue - without having to create custom error formatters, or run debugger.
Install
Install using npm:
$ npm install @grimen/mybad
Install using yarn:
$ yarn add @grimen/mybad
Use
Very basic example:
const mybad = require('@grimen/mybad')
class ToMuchError extends mybad.Error {}
function printMoney(stash) {
try {
if (typeof stash === 'string' && stash.length > 13) {
throw new RangeError(`Too much money to print: ${stash}`)
}
console.log(`PRINT ${stash.join('')}`)
} catch (error) {
throw new ToMuchError(error, {
message: 'Out of money printing ink...',
id: Date.now(), // well, should maybe be unique...
key: 'too_much',
code: 400,
details: {
stash,
},
})
}
}
let amount = ''
const range = (to) => {
return [...Array(to).keys()]
}
for (let dollar of range(42)) {
amount += '