Declarative errors throwing for Node.js
- boolean predicate – a result of some condition. Error will threw if predicate is
true
. - object error – some error object
- 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
- 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
Do the same things as invariant
, but returns Promise that will be rejected when predicate is true.
This function have same API as invariant
.
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))