GithubHelp home page GithubHelp logo

isabella232 / create-boom-error Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 62 KB

A simple Node.js library for easily creating classed Boom errors in Hapi applications.

License: MIT License

JavaScript 100.00%

create-boom-error's Introduction

create-boom-error npm version Build Status

A simple Node.js library for easily creating classed Boom errors in Hapi applications.

Installation

yarn add 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

var createBoomError = require('create-boom-error');

var MyError = createBoomError('MyError', 404, 'simple message', 'not_found');

var 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

var MyError = createBoomError('MyError', 404, function (num) {
  return 'You must have more than ' + num + ' coins.';
});

var err = new MyError(4);

// err now has the dynamic message
err.message // => 'You must have more than 4 coins.'

Automatically exporting an error

This is a useful shortcut if you have a file in your application where you want to declare all your errors and automatically export them. Simply call .bind(exports) when requiring the create-boom-error library.

// in customErrors.js
var createBoomError = require('create-boom-error').bind(exports);

createBoomError('TestError', 400, 'test message');

This error is automatically exported in customError.js:

// in another file
var CustomErrors = require('./customErrors');

var err = new CustomErrors.TestError();

err instanceof CustomErrors.TestError // => true

Development

Run tests

yarn test

create-boom-error's People

Contributors

ajpawlicki avatar leore avatar mgartner avatar nathankhuu avatar pdufour avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.