Add additional message to internal invariants
The message tells users that the error is likely due to a bug in React, and that they should file an issue.
Andrew Clark committed
Feb 6, 2017 at 13:52 UTC
915a3e88a25690bd13e1e740f6de60b698858f82
7 files changed
+92
-33
src/renderers/shared/fiber/ReactChildFiber.js
+11
-3
@@ -72,6 +72,9 @@ const {
72
Deletion,
73
} = ReactTypeOfSideEffect;
74
75
+const internalErrorMessage =
76
+ 'This error is likely caused by a bug in React. Please file an issue.';
77
+
78
function coerceRef(current: ?Fiber, element: ReactElement) {
79
let mixedRef = element.ref;
80
if (mixedRef != null && typeof mixedRef !== 'function') {
@@ -88,7 +91,11 @@ function coerceRef(current: ?Fiber, element: ReactElement) {
91
inst = (owner : any).getPublicInstance();
92
}
93
}
91
- invariant(inst, 'Missing owner for string ref %s', mixedRef);
94
+ invariant(
95
+ inst, 'Missing owner for string ref %s. (%s)',
96
+ mixedRef,
97
+ internalErrorMessage
98
+ );
99
const stringRef = String(mixedRef);
100
// Check if previous string ref matches new string ref
101
if (current && current.ref && current.ref._stringRef === stringRef) {
@@ -799,7 +806,8 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
806
const iteratorFn = getIteratorFn(newChildrenIterable);
807
invariant(
808
typeof iteratorFn === 'function',
802
- 'An object is not an iterable.'
809
+ 'An object is not an iterable. (%s)',
810
+ internalErrorMessage
811
);
812
813
if (__DEV__) {
@@ -819,7 +827,7 @@ function ChildReconciler(shouldClone, shouldTrackSideEffects) {
827
const newChildren = iteratorFn.call(newChildrenIterable);
828
invariant(
829
newChildren != null,
822
- 'An iterable object provided no iterator.'
830
+ 'An iterable object provided no iterator.',
831
);
832
833
let resultingFirstChild : ?Fiber = null;
src/renderers/shared/fiber/ReactFiberBeginWork.js
+18
-6
@@ -71,6 +71,9 @@ if (__DEV__) {
71
var warnedAboutStatelessRefs = {};
72
}
73
74
+const internalErrorMessage =
75
+ 'This error is likely caused by a bug in React. Please file an issue.';
76
+
77
module.exports = function<T, P, I, TI, PI, C, CX, PL>(
78
config : HostConfig<T, P, I, TI, PI, C, CX, PL>,
79
hostContext : HostContext<C, CX>,
@@ -344,7 +347,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
347
nextProps = memoizedProps;
348
invariant(
349
nextProps !== null,
347
- 'We should always have pending or current props.'
350
+ 'We should always have pending or current props. (%s)',
351
+ internalErrorMessage
352
);
353
}
354
} else if (nextProps === null || memoizedProps === nextProps) {
@@ -446,7 +450,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
450
function mountIndeterminateComponent(current, workInProgress, priorityLevel) {
451
invariant(
452
current === null,
449
- 'An indeterminate component should never have mounted'
453
+ 'An indeterminate component should never have mounted. (%s)',
454
+ internalErrorMessage
455
);
456
var fn = workInProgress.type;
457
var props = workInProgress.pendingProps;
@@ -516,7 +521,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
521
nextCoroutine = current && current.memoizedProps;
522
invariant(
523
nextCoroutine != null,
519
- 'We should always have pending or current props.'
524
+ 'We should always have pending or current props. (%s)',
525
+ internalErrorMessage
526
);
527
}
528
} else if (nextCoroutine === null || workInProgress.memoizedProps === nextCoroutine) {
@@ -581,7 +587,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
587
nextChildren = current && current.memoizedProps;
588
invariant(
589
nextChildren != null,
584
- 'We should always have pending or current props.'
590
+ 'We should always have pending or current props. (%s)',
591
+ internalErrorMessage
592
);
593
}
594
} else if (nextChildren === null || workInProgress.memoizedProps === nextChildren) {
@@ -732,7 +739,11 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
739
case Fragment:
740
return updateFragment(current, workInProgress);
741
default:
735
- invariant(false, 'Unknown unit of work tag');
742
+ invariant(
743
+ false,
744
+ 'Unknown unit of work tag. (%s)',
745
+ internalErrorMessage
746
+ );
747
}
748
}
749
@@ -740,7 +751,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
751
invariant(
752
workInProgress.tag === ClassComponent ||
753
workInProgress.tag === HostRoot,
743
- 'Invalid type of work'
754
+ 'Invalid type of work. (%s)',
755
+ internalErrorMessage
756
);
757
758
// Add an error effect so we can handle the error during the commit phase
src/renderers/shared/fiber/ReactFiberClassComponent.js
+9
-3
@@ -40,6 +40,9 @@ var invariant = require('invariant');
40
41
const isArray = Array.isArray;
42
43
+const internalErrorMessage =
44
+ 'This error is likely caused by a bug in React. Please file an issue.';
45
+
46
module.exports = function(
47
scheduleUpdate : (fiber : Fiber, priorityLevel : PriorityLevel) => void,
48
getPriorityContext : () => PriorityLevel,
@@ -259,7 +262,8 @@ module.exports = function(
262
let props = workInProgress.pendingProps;
263
invariant(
264
props,
262
- 'There must be pending props for an initial mount.'
265
+ 'There must be pending props for an initial mount. (%s)',
266
+ internalErrorMessage
267
);
268
269
const unmaskedContext = getUnmaskedContext(workInProgress);
@@ -301,7 +305,8 @@ module.exports = function(
305
newProps = workInProgress.memoizedProps;
306
invariant(
307
newProps != null,
304
- 'There should always be pending or memoized props.'
308
+ 'There should always be pending or memoized props. (%s)',
309
+ internalErrorMessage
310
);
311
}
312
const newUnmaskedContext = getUnmaskedContext(workInProgress);
@@ -367,7 +372,8 @@ module.exports = function(
372
newProps = oldProps;
373
invariant(
374
newProps != null,
370
- 'There should always be pending or memoized props.'
375
+ 'There should always be pending or memoized props. (%s)',
376
+ internalErrorMessage
377
);
378
}
379
const oldContext = instance.context;
src/renderers/shared/fiber/ReactFiberCommitWork.js
+15
-6
@@ -36,6 +36,9 @@ var {
36
37
var invariant = require('invariant');
38
39
+const internalErrorMessage =
40
+ 'This error is likely caused by a bug in React. Please file an issue.';
41
+
42
module.exports = function<T, P, I, TI, PI, C, CX, PL>(
43
config : HostConfig<T, P, I, TI, PI, C, CX, PL>,
44
captureError : (failedFiber : Fiber, error: Error) => ?Fiber
@@ -98,7 +101,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
101
}
102
invariant(
103
false,
101
- 'Expected to find a host parent.'
104
+ 'Expected to find a host parent. (%s)',
105
+ internalErrorMessage
106
);
107
}
108
@@ -112,7 +116,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
116
}
117
invariant(
118
false,
115
- 'Expected to find a host parent.'
119
+ 'Expected to find a host parent. (%s)',
120
+ internalErrorMessage
121
);
122
}
123
@@ -183,7 +188,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
188
default:
189
invariant(
190
false,
186
- 'Invalid host parent fiber.'
191
+ 'Invalid host parent fiber. (%s)',
192
+ internalErrorMessage
193
);
194
}
195
if (parentFiber.effectTag & ContentReset) {
@@ -387,7 +393,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
393
case HostText: {
394
invariant(
395
finishedWork.stateNode !== null && current != null,
390
- 'This should only be done during updates.'
396
+ 'This should only be done during updates. (%s)',
397
+ internalErrorMessage
398
);
399
const textInstance : TI = finishedWork.stateNode;
400
const newText : string = finishedWork.memoizedProps;
@@ -404,7 +411,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
411
default: {
412
invariant(
413
false,
407
- 'This unit of work tag should not have side-effects.'
414
+ 'This unit of work tag should not have side-effects. (%s)',
415
+ internalErrorMessage
416
);
417
}
418
}
@@ -469,7 +477,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
477
default: {
478
invariant(
479
false,
472
- 'This unit of work tag should not have side-effects.'
480
+ 'This unit of work tag should not have side-effects. (%s)',
481
+ internalErrorMessage
482
);
483
}
484
}
src/renderers/shared/fiber/ReactFiberCompleteWork.js
+13
-5
@@ -48,6 +48,9 @@ if (__DEV__) {
48
49
var invariant = require('invariant');
50
51
+const internalErrorMessage =
52
+ 'This error is likely caused by a bug in React. Please file an issue.';
53
+
54
module.exports = function<T, P, I, TI, PI, C, CX, PL>(
55
config : HostConfig<T, P, I, TI, PI, C, CX, PL>,
56
hostContext : HostContext<C, CX>,
@@ -119,7 +122,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
122
var coroutine = (workInProgress.memoizedProps : ?ReactCoroutine);
123
invariant(
124
coroutine,
122
- 'Should be resolved by now'
125
+ 'Should be resolved by now. (%s)',
126
+ internalErrorMessage
127
);
128
129
// First step of the coroutine has completed. Now we need to do the second.
@@ -237,7 +241,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
241
if (!newProps) {
242
invariant(
243
workInProgress.stateNode !== null,
240
- 'We must have new props for new mounts'
244
+ 'We must have new props for new mounts. (%s)',
245
+ internalErrorMessage
246
);
247
// This can happen when we abort work.
248
return null;
@@ -286,7 +291,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
291
if (typeof newText !== 'string') {
292
invariant(
293
workInProgress.stateNode !== null,
289
- 'We must have new props for new mounts'
294
+ 'We must have new props for new mounts. (%s)',
295
+ internalErrorMessage
296
);
297
// This can happen when we abort work.
298
return null;
@@ -319,13 +325,15 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
325
case IndeterminateComponent:
326
invariant(
327
false,
322
- 'An indeterminate component should have become determinate before completing.'
328
+ 'An indeterminate component should have become determinate before completing. (%s)',
329
+ internalErrorMessage
330
);
331
// eslint-disable-next-line no-fallthrough
332
default:
333
invariant(
334
false,
328
- 'Unknown unit of work tag'
335
+ 'Unknown unit of work tag. (%s)',
336
+ internalErrorMessage
337
);
338
}
339
}
src/renderers/shared/fiber/ReactFiberHostContext.js
+9
-3
@@ -36,6 +36,9 @@ export type HostContext<C, CX> = {
36
resetHostContainer() : void,
37
};
38
39
+const internalErrorMessage =
40
+ 'This error is likely caused by a bug in React. Please file an issue.';
41
+
42
module.exports = function<T, P, I, TI, PI, C, CX, PL>(
43
config : HostConfig<T, P, I, TI, PI, C, CX, PL>
44
) : HostContext<C, CX> {
@@ -52,7 +55,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
55
const rootInstance = rootInstanceStackCursor.current;
56
invariant(
57
rootInstance != null,
55
- 'Expected root container to exist.'
58
+ 'Expected root container to exist. (%s)',
59
+ internalErrorMessage
60
);
61
return rootInstance;
62
}
@@ -80,7 +84,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
84
const context = contextStackCursor.current;
85
invariant(
86
context != null,
83
- 'Expected host context to exist.'
87
+ 'Expected host context to exist. (%s)',
88
+ internalErrorMessage
89
);
90
return context;
91
}
@@ -89,7 +94,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
94
const rootInstance = rootInstanceStackCursor.current;
95
invariant(
96
rootInstance != null,
92
- 'Expected root host context to exist.'
97
+ 'Expected root host context to exist. (%s)',
98
+ internalErrorMessage
99
);
100
101
const context = contextStackCursor.current || emptyObject;
src/renderers/shared/fiber/ReactFiberScheduler.js
+17
-7
@@ -92,6 +92,9 @@ if (__DEV__) {
92
93
var timeHeuristicForUnitOfWork = 1;
94
95
+const internalErrorMessage =
96
+ 'This error is likely caused by a bug in React. Please file an issue.';
97
+
98
module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P, I, TI, PI, C, CX, PL>) {
99
const hostContext = ReactFiberHostContext(config);
100
const { popHostContainer, popHostContext, resetHostContainer } = hostContext;
@@ -345,7 +348,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
348
invariant(
349
root.current !== finishedWork,
350
'Cannot commit the same tree as before. This is probably a bug ' +
348
- 'related to the return field.'
351
+ 'related to the return field. (%s)',
352
+ internalErrorMessage
353
);
354
355
// Updates that occur during the commit phase should have Task priority
@@ -381,7 +385,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
385
} catch (error) {
386
invariant(
387
nextEffect != null,
384
- 'Should have next effect.'
388
+ 'Should have next effect. (%s)',
389
+ internalErrorMessage
390
);
391
captureError(nextEffect, error);
392
// Clean-up
@@ -410,7 +415,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
415
} catch (error) {
416
invariant(
417
nextEffect != null,
413
- 'Should have next effect.'
418
+ 'Should have next effect. (%s)',
419
+ internalErrorMessage
420
);
421
captureError(nextEffect, error);
422
if (nextEffect) {
@@ -710,7 +716,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
716
function performWork(priorityLevel : PriorityLevel, deadline : Deadline | null) {
717
invariant(
718
!isPerformingWork,
713
- 'performWork was called recursively.'
719
+ 'performWork was called recursively. (%s)',
720
+ internalErrorMessage
721
);
722
isPerformingWork = true;
723
const isPerformingDeferredWork = Boolean(deadline);
@@ -722,7 +729,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
729
while (priorityLevel !== NoWork && !fatalError) {
730
invariant(
731
deadline || (priorityLevel < HighPriority),
725
- 'Cannot perform deferred work without a deadline.'
732
+ 'Cannot perform deferred work without a deadline. (%s)',
733
+ internalErrorMessage
734
);
735
736
// Before starting any work, check to see if there are any pending
@@ -993,7 +1001,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
1001
1002
invariant(
1003
capturedError != null,
996
- 'No error for given unit of work.'
1004
+ 'No error for given unit of work. (%s)',
1005
+ internalErrorMessage
1006
);
1007
1008
let error;
@@ -1029,7 +1038,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(config : HostConfig<T, P,
1038
default:
1039
invariant(
1040
false,
1032
- 'Invalid type of work.'
1041
+ 'Invalid type of work. (%s)',
1042
+ internalErrorMessage
1043
);
1044
}
1045
}