https://github.com/octet-stream/invariant
Declarative errors throwing for Node.js
https://github.com/octet-stream/invariant
error-handling error-throwing format invariant javascript javascript-library nodejs sprintf
Last synced: 9 months ago
JSON representation
Declarative errors throwing for Node.js
- Host: GitHub
- URL: https://github.com/octet-stream/invariant
- Owner: octet-stream
- License: mit
- Created: 2017-07-28T14:08:30.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-10T03:28:14.000Z (about 3 years ago)
- Last Synced: 2025-04-19T09:08:15.842Z (10 months ago)
- Topics: error-handling, error-throwing, format, invariant, javascript, javascript-library, nodejs, sprintf
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@octetstream/invariant
- Size: 501 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# @octetstream/invariant
Declarative errors throwing for Node.js
[](https://david-dm.org/octet-stream/invariant)
[](https://david-dm.org/octet-stream/invariant?type=dev)
[](https://travis-ci.org/octet-stream/invariant)
[](https://codecov.io/github/octet-stream/invariant?branch=master)
## API
### `invariant(predicate, error) -> {void}`
- **boolean** predicate – a result of some condition. Error will threw if predicate is `true`.
- **object** error – some error object
### `invatiant(predicate, message[, ...format]) -> {void}`
- **boolean** predicate – a result of some condition. Error will threw if predicate is `true`.
- **string** message – an error message
- **any** format – see more about the format in a [sprintf-js documentation](https://github.com/alexei/sprintf.js)
### `invariant(predicate, Error, message[, ...format]) -> {void}`
- **boolean** predicate – a result of some condition. Error will threw if predicate is `true`.
- **Function** Error – custom error class that will be used as an error constructor
- **string** message – an error message
- **any** format – see more about the format in a [sprintf-js documentation](https://github.com/alexei/sprintf.js)
### `invariant.reject(predicate, message[, ...format]) -> {Promise}`
Do the same things as `invariant`, but returns Promise that will be rejected when predicate is true.
This function have same API as `invariant`.
## Example
```js
import invariant from "@octetstream/invariant"
// some of your code...
// Will threw a TypeError if "value" is not a string
invariant(typeof value !== "string", TypeError, "The value should be a string, but given type is: %s", typeof value)
invariant(typeof value !== "string", "The value should be a string, but given type is: %s", typeof value)
invariant(typeof value !== "string", new TypeError(`The value should be a string, but given type is: ${typeof value}`))
// Will return rejected Promise instead of throw error synchronously
invariant.reject(typeof value !== "string", TypeError, "The value should be a string, but given type is: %s", typeof value)
.catch(err => console.error(err))
```