create-boom-error
A simple Node.js library for easily creating classed Boom errors in Hapi applications.
Installation
npm install create-boom-error
Usage
createBoomError(name, statusCode, [message], [code])
Creates a Boom error.
name
- The name of the error.statusCode
- the integer status code of the Boom errormessage
- an optional string or function which returns a stringcode
- an optional machine-keyable error status string
Create a simple error
const { createBoomError } = require('create-boom-error');
const MyError = createBoomError('MyError', 404, 'simple message', 'not_found');
const err = new MyError();
err instanceof MyError
err.code
Note that if the optional code
argument is NOT passed in then the .code
field attached to the error will default to a decamelized, snake case version of the name
. For example:
const MyError = createBoomError('MyError', 404, 'simple message');
const err = new MyError();
err instanceof MyError
err.code
Create an error with a dynamic message
const MyError = createBoomError('MyError', 404, (num) => `You must have more than ${num} coins.`);
const err = new MyError(4);
err.message
Development
Run tests
npm run test