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

0.2.1 • Public • Published

boom-invariant

This is an implementation of Facebook's invariant() function built on top of Boom, a library for constructing error objects with HTTP status codes. Usage and behavior are similar to the original, but with two noteworthy changes:

  • Error messages are not formatted for you. You should use ES6 template literals to format your error message when calling the function.
  • The third argument is an optional HTTP status code. By default the status code is 400 Bad Request.

Installation

If you're using npm:

npm i --save boom-invariant

If you're using yarn:

yarn add boom-invariant`

Usage

// Throws a Boom error with a 400 `statusCode` when `someCondition` is not truthy.
invariant(someCondition, 'Uh oh! Condition was not met!');
// Throws a Boom error with a 418 `statusCode` when `someCondition` is not truthy.
invariant(someCondition, 'Uh oh! Condition was not met because I\'m a teapot!', 418);

Or if you don't enjoy recalling HTTP status codes:

// Throws a Boom error with a 400 `statusCode` when someCondition is not truthy.
invariant(someCondition, 'Uh oh! I can\'t find the resource!', Boom.notFound);

Note that the error message argument is required.

Readme

Keywords

none

Package Sidebar

Install

npm i boom-invariant

Weekly Downloads

3

Version

0.2.1

License

MIT

Unpacked Size

11.5 kB

Total Files

12

Last publish

Collaborators

  • swac