create-boom-error
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

create-boom-error npm version Build Status

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 error
  • message - an optional string or function which returns a string
  • code - 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 is an instance of MyError making it easy to check in tests
err instanceof MyError // => true

// err.code will match the code argument passed in
err.code // => 'not_found'

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 is an instance of MyError making it easy to check in tests
err instanceof MyError // => true

// err.code will return the name argument decamelized and in snake case
err.code // => 'my_error'

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 now has the dynamic message
err.message // => 'You must have more than 4 coins.'

Development

Run tests

npm run test

Readme

Keywords

Package Sidebar

Install

npm i create-boom-error

Weekly Downloads

351

Version

3.0.2

License

MIT

Unpacked Size

9.67 kB

Total Files

8

Last publish

Collaborators

  • erik.forsman-lob
  • jkleung11
  • tanya.sah
  • hunteryoakum
  • rdimouro7373
  • lobstertroy
  • joshnkoy
  • michel_lob
  • haroutrs
  • kjones_lob
  • eamon-barisone
  • nathanielwaldschmidtlob
  • zach.reed
  • richpodrazalob
  • kencrim
  • jorgelob
  • nick-place-lob
  • andrew.guterres
  • ajorczak
  • hanqingchen-lob
  • juan.frissdekereki
  • mmorgan-lob
  • karankwatra-lob
  • joey-bates-lob
  • luke.birdeau
  • mg-lob
  • vmangwani
  • sachinlob
  • nick.perri
  • siddharthpant92
  • bethqiang
  • kplob
  • samkitsheth95
  • erin-doyle
  • rich_seviora_lob
  • jfdavidson
  • meussdorffer
  • shannamurry
  • davidnutting-lob
  • amaan_lob
  • team.platform.lob.com
  • elijah-lob
  • jlowsley-lob
  • barnabygo
  • james.cho
  • douglaje
  • lob-owner
  • graeme.lowe.lob