@@ -140,6 +140,7 @@ import {
140
140
} from './ReactHookEffectTags' ;
141
141
import { didWarnAboutReassigningProps } from './ReactFiberBeginWork.new' ;
142
142
import { doesFiberContain } from './ReactFiberTreeReflection' ;
143
+ import { invokeGuardedCallback } from 'shared/ReactErrorUtils' ;
143
144
144
145
let didWarnAboutUndefinedSnapshotBeforeUpdate : Set < mixed > | null = null ;
145
146
if ( __DEV__ ) {
@@ -160,6 +161,19 @@ let nextEffect: Fiber | null = null;
160
161
let inProgressLanes : Lanes | null = null ;
161
162
let inProgressRoot : FiberRoot | null = null ;
162
163
164
+ function reportUncaughtErrorInDEV ( error ) {
165
+ // Wrapping each small part of the commit phase into a guarded
166
+ // callback is a bit too slow (https://github.com/facebook/react/pull/21666).
167
+ // But we rely on it to surface errors to DEV tools like overlays
168
+ // (https://github.com/facebook/react/issues/21712).
169
+ // As a compromise, rethrow only caught errors in a guard.
170
+ if ( __DEV__ ) {
171
+ invokeGuardedCallback ( null , ( ) => {
172
+ throw error ;
173
+ } ) ;
174
+ }
175
+ }
176
+
163
177
const callComponentWillUnmountWithTimer = function ( current , instance ) {
164
178
instance . props = current . memoizedProps ;
165
179
instance . state = current . memoizedState ;
@@ -186,8 +200,9 @@ function safelyCallCommitHookLayoutEffectListMount(
186
200
) {
187
201
try {
188
202
commitHookEffectListMount ( HookLayout , current ) ;
189
- } catch ( unmountError ) {
190
- captureCommitPhaseError ( current , nearestMountedAncestor , unmountError ) ;
203
+ } catch ( error ) {
204
+ reportUncaughtErrorInDEV ( error ) ;
205
+ captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
191
206
}
192
207
}
193
208
@@ -199,8 +214,9 @@ function safelyCallComponentWillUnmount(
199
214
) {
200
215
try {
201
216
callComponentWillUnmountWithTimer ( current , instance ) ;
202
- } catch ( unmountError ) {
203
- captureCommitPhaseError ( current , nearestMountedAncestor , unmountError ) ;
217
+ } catch ( error ) {
218
+ reportUncaughtErrorInDEV ( error ) ;
219
+ captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
204
220
}
205
221
}
206
222
@@ -212,17 +228,19 @@ function safelyCallComponentDidMount(
212
228
) {
213
229
try {
214
230
instance . componentDidMount ( ) ;
215
- } catch ( unmountError ) {
216
- captureCommitPhaseError ( current , nearestMountedAncestor , unmountError ) ;
231
+ } catch ( error ) {
232
+ reportUncaughtErrorInDEV ( error ) ;
233
+ captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
217
234
}
218
235
}
219
236
220
237
// Capture errors so they don't interrupt mounting.
221
238
function safelyAttachRef ( current : Fiber , nearestMountedAncestor : Fiber | null ) {
222
239
try {
223
240
commitAttachRef ( current ) ;
224
- } catch ( unmountError ) {
225
- captureCommitPhaseError ( current , nearestMountedAncestor , unmountError ) ;
241
+ } catch ( error ) {
242
+ reportUncaughtErrorInDEV ( error ) ;
243
+ captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
226
244
}
227
245
}
228
246
@@ -246,6 +264,7 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) {
246
264
ref ( null ) ;
247
265
}
248
266
} catch ( error ) {
267
+ reportUncaughtErrorInDEV ( error ) ;
249
268
captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
250
269
}
251
270
} else {
@@ -262,6 +281,7 @@ function safelyCallDestroy(
262
281
try {
263
282
destroy ( ) ;
264
283
} catch ( error ) {
284
+ reportUncaughtErrorInDEV ( error ) ;
265
285
captureCommitPhaseError ( current , nearestMountedAncestor , error ) ;
266
286
}
267
287
}
@@ -323,6 +343,7 @@ function commitBeforeMutationEffects_complete() {
323
343
try {
324
344
commitBeforeMutationEffectsOnFiber ( fiber ) ;
325
345
} catch ( error ) {
346
+ reportUncaughtErrorInDEV ( error ) ;
326
347
captureCommitPhaseError ( fiber , fiber . return , error ) ;
327
348
}
328
349
resetCurrentDebugFiberInDEV ( ) ;
@@ -2065,6 +2086,7 @@ function commitMutationEffects_begin(root: FiberRoot) {
2065
2086
try {
2066
2087
commitDeletion ( root , childToDelete , fiber ) ;
2067
2088
} catch ( error ) {
2089
+ reportUncaughtErrorInDEV ( error ) ;
2068
2090
captureCommitPhaseError ( childToDelete , fiber , error ) ;
2069
2091
}
2070
2092
}
@@ -2087,6 +2109,7 @@ function commitMutationEffects_complete(root: FiberRoot) {
2087
2109
try {
2088
2110
commitMutationEffectsOnFiber ( fiber , root ) ;
2089
2111
} catch ( error ) {
2112
+ reportUncaughtErrorInDEV ( error ) ;
2090
2113
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2091
2114
}
2092
2115
resetCurrentDebugFiberInDEV ( ) ;
@@ -2329,6 +2352,7 @@ function commitLayoutMountEffects_complete(
2329
2352
try {
2330
2353
commitLayoutEffectOnFiber ( root , current , fiber , committedLanes ) ;
2331
2354
} catch ( error ) {
2355
+ reportUncaughtErrorInDEV ( error ) ;
2332
2356
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2333
2357
}
2334
2358
resetCurrentDebugFiberInDEV ( ) ;
@@ -2382,6 +2406,7 @@ function commitPassiveMountEffects_complete(
2382
2406
try {
2383
2407
commitPassiveMountOnFiber ( root , fiber ) ;
2384
2408
} catch ( error ) {
2409
+ reportUncaughtErrorInDEV ( error ) ;
2385
2410
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2386
2411
}
2387
2412
resetCurrentDebugFiberInDEV ( ) ;
@@ -2664,6 +2689,7 @@ function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
2664
2689
try {
2665
2690
commitHookEffectListMount ( HookLayout | HookHasEffect , fiber ) ;
2666
2691
} catch ( error ) {
2692
+ reportUncaughtErrorInDEV ( error ) ;
2667
2693
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2668
2694
}
2669
2695
break ;
@@ -2673,6 +2699,7 @@ function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
2673
2699
try {
2674
2700
instance . componentDidMount ( ) ;
2675
2701
} catch ( error ) {
2702
+ reportUncaughtErrorInDEV ( error ) ;
2676
2703
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2677
2704
}
2678
2705
break ;
@@ -2692,6 +2719,7 @@ function invokePassiveEffectMountInDEV(fiber: Fiber): void {
2692
2719
try {
2693
2720
commitHookEffectListMount ( HookPassive | HookHasEffect , fiber ) ;
2694
2721
} catch ( error ) {
2722
+ reportUncaughtErrorInDEV ( error ) ;
2695
2723
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2696
2724
}
2697
2725
break ;
@@ -2715,6 +2743,7 @@ function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void {
2715
2743
fiber . return ,
2716
2744
) ;
2717
2745
} catch ( error ) {
2746
+ reportUncaughtErrorInDEV ( error ) ;
2718
2747
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2719
2748
}
2720
2749
break ;
@@ -2745,6 +2774,7 @@ function invokePassiveEffectUnmountInDEV(fiber: Fiber): void {
2745
2774
fiber . return ,
2746
2775
) ;
2747
2776
} catch ( error ) {
2777
+ reportUncaughtErrorInDEV ( error ) ;
2748
2778
captureCommitPhaseError ( fiber , fiber . return , error ) ;
2749
2779
}
2750
2780
}
0 commit comments