1 |
3a515b92
|
cagy
|
// @flow
|
2 |
|
|
const isProduction: boolean = process.env.NODE_ENV === 'production';
|
3 |
|
|
const prefix: string = 'Invariant failed';
|
4 |
|
|
|
5 |
|
|
// Throw an error if the condition fails
|
6 |
|
|
// Strip out error messages for production
|
7 |
|
|
// > Not providing an inline default argument for message as the result is smaller
|
8 |
|
|
export default function invariant(
|
9 |
|
|
condition: any,
|
10 |
|
|
message?: string,
|
11 |
|
|
): asserts condition {
|
12 |
|
|
if (condition) {
|
13 |
|
|
return;
|
14 |
|
|
}
|
15 |
|
|
// Condition not passed
|
16 |
|
|
|
17 |
|
|
// In production we strip the message but still throw
|
18 |
|
|
if (isProduction) {
|
19 |
|
|
throw new Error(prefix);
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
// When not in production we allow the message to pass through
|
23 |
|
|
// *This block will be removed in production builds*
|
24 |
|
|
throw new Error(`${prefix}: ${message || ''}`);
|
25 |
|
|
}
|