Skip to content

Commit ddd0d91

Browse files
committed
Inline ReactErrorUtils
This is pretty simple now.
1 parent 61b2452 commit ddd0d91

File tree

6 files changed

+61
-152
lines changed

6 files changed

+61
-152
lines changed

packages/react-dom-bindings/src/events/DOMPluginEventSystem.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ import {
5555
enableFloat,
5656
enableFormActions,
5757
} from 'shared/ReactFeatureFlags';
58-
import {
59-
invokeGuardedCallbackAndCatchFirstError,
60-
rethrowCaughtError,
61-
} from 'shared/ReactErrorUtils';
6258
import {createEventListenerWrapperWithPriority} from './ReactDOMEventListener';
6359
import {
6460
removeEventListener,
@@ -234,14 +230,25 @@ export const nonDelegatedEvents: Set<DOMEventName> = new Set([
234230
...mediaEventTypes,
235231
]);
236232

233+
let hasError: boolean = false;
234+
let caughtError: mixed = null;
235+
237236
function executeDispatch(
238237
event: ReactSyntheticEvent,
239238
listener: Function,
240239
currentTarget: EventTarget,
241240
): void {
242-
const type = event.type || 'unknown-event';
243241
event.currentTarget = currentTarget;
244-
invokeGuardedCallbackAndCatchFirstError(type, listener, undefined, event);
242+
try {
243+
listener(event);
244+
} catch (error) {
245+
if (!hasError) {
246+
hasError = true;
247+
caughtError = error;
248+
} else {
249+
// TODO: Make sure this error gets logged somehow.
250+
}
251+
}
245252
event.currentTarget = null;
246253
}
247254

@@ -283,7 +290,12 @@ export function processDispatchQueue(
283290
// event system doesn't use pooling.
284291
}
285292
// This would be a good time to rethrow if any of the event handlers threw.
286-
rethrowCaughtError();
293+
if (hasError) {
294+
const error = caughtError;
295+
hasError = false;
296+
caughtError = null;
297+
throw error;
298+
}
287299
}
288300

289301
function dispatchEventsForPlugins(

packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4643,7 +4643,7 @@ describe('ReactDOMFizzServer', () => {
46434643
await waitForAll([]);
46444644
});
46454645

4646-
it('does not invokeGuardedCallback for errors after the first hydration error', async () => {
4646+
it('does not log for errors after the first hydration error', async () => {
46474647
// We can't use the toErrorDev helper here because this is async.
46484648
const originalConsoleError = console.error;
46494649
const mockError = jest.fn();
@@ -4740,7 +4740,7 @@ describe('ReactDOMFizzServer', () => {
47404740
}
47414741
});
47424742

4743-
it('does not invokeGuardedCallback for errors after a preceding fiber suspends', async () => {
4743+
it('does not log for errors after a preceding fiber suspends', async () => {
47444744
// We can't use the toErrorDev helper here because this is async.
47454745
const originalConsoleError = console.error;
47464746
const mockError = jest.fn();

packages/react-dom/src/test-utils/ReactTestUtils.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ import {
2121
} from 'react-reconciler/src/ReactWorkTags';
2222
import {SyntheticEvent} from 'react-dom-bindings/src/events/SyntheticEvent';
2323
import {ELEMENT_NODE} from 'react-dom-bindings/src/client/HTMLNodeType';
24-
import {
25-
rethrowCaughtError,
26-
invokeGuardedCallbackAndCatchFirstError,
27-
} from 'shared/ReactErrorUtils';
2824
import {enableFloat} from 'shared/ReactFeatureFlags';
2925
import assign from 'shared/assign';
3026
import isArray from 'shared/isArray';
@@ -354,16 +350,25 @@ function nativeTouchData(x, y) {
354350
// EventPropagator.js, as they deviated from ReactDOM's newer
355351
// implementations.
356352

353+
let hasError: boolean = false;
354+
let caughtError: mixed = null;
355+
357356
/**
358357
* Dispatch the event to the listener.
359358
* @param {SyntheticEvent} event SyntheticEvent to handle
360359
* @param {function} listener Application-level callback
361360
* @param {*} inst Internal component instance
362361
*/
363362
function executeDispatch(event, listener, inst) {
364-
const type = event.type || 'unknown-event';
365363
event.currentTarget = getNodeFromInstance(inst);
366-
invokeGuardedCallbackAndCatchFirstError(type, listener, undefined, event);
364+
try {
365+
listener(event);
366+
} catch (error) {
367+
if (!hasError) {
368+
hasError = true;
369+
caughtError = error;
370+
}
371+
}
367372
event.currentTarget = null;
368373
}
369374

@@ -619,7 +624,12 @@ function makeSimulator(eventType) {
619624
// do that since we're by-passing it here.
620625
enqueueStateRestore(domNode);
621626
executeDispatchesAndRelease(event);
622-
rethrowCaughtError();
627+
if (hasError) {
628+
const error = caughtError;
629+
hasError = false;
630+
caughtError = null;
631+
throw error;
632+
}
623633
});
624634
restoreStateIfNeeded();
625635
};

packages/react-native-renderer/src/legacy-events/EventBatching.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
* @flow
77
*/
88

9-
import {rethrowCaughtError} from 'shared/ReactErrorUtils';
10-
119
import type {ReactSyntheticEvent} from './ReactSyntheticEventType';
1210
import accumulateInto from './accumulateInto';
1311
import forEachAccumulated from './forEachAccumulated';
14-
import {executeDispatchesInOrder} from './EventPluginUtils';
12+
import {executeDispatchesInOrder, rethrowCaughtError} from './EventPluginUtils';
1513

1614
/**
1715
* Internal queue of events that have accumulated their dispatches and are

packages/react-native-renderer/src/legacy-events/EventPluginUtils.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {invokeGuardedCallbackAndCatchFirstError} from 'shared/ReactErrorUtils';
98
import isArray from 'shared/isArray';
109

10+
let hasError = false;
11+
let caughtError = null;
12+
1113
export let getFiberCurrentPropsFromNode = null;
1214
export let getInstanceFromNode = null;
1315
export let getNodeFromInstance = null;
@@ -62,9 +64,17 @@ function validateEventDispatches(event) {
6264
* @param {*} inst Internal component instance
6365
*/
6466
export function executeDispatch(event, listener, inst) {
65-
const type = event.type || 'unknown-event';
6667
event.currentTarget = getNodeFromInstance(inst);
67-
invokeGuardedCallbackAndCatchFirstError(type, listener, undefined, event);
68+
try {
69+
listener(event);
70+
} catch (error) {
71+
if (!hasError) {
72+
hasError = true;
73+
caughtError = error;
74+
} else {
75+
// TODO: Make sure this error gets logged somehow.
76+
}
77+
}
6878
event.currentTarget = null;
6979
}
7080

@@ -170,3 +180,12 @@ export function executeDirectDispatch(event) {
170180
export function hasDispatches(event) {
171181
return !!event._dispatchListeners;
172182
}
183+
184+
export function rethrowCaughtError() {
185+
if (hasError) {
186+
const error = caughtError;
187+
hasError = false;
188+
caughtError = null;
189+
throw error;
190+
}
191+
}

packages/shared/ReactErrorUtils.js

-130
This file was deleted.

0 commit comments

Comments
 (0)