Skip to content

Commit

Permalink
Inline ReactErrorUtils
Browse files Browse the repository at this point in the history
This is pretty simple now.
  • Loading branch information
sebmarkbage committed Mar 11, 2024
1 parent 6455dbc commit 944275e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 152 deletions.
26 changes: 19 additions & 7 deletions packages/react-dom-bindings/src/events/DOMPluginEventSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ import {
enableFloat,
enableFormActions,
} from 'shared/ReactFeatureFlags';
import {
invokeGuardedCallbackAndCatchFirstError,
rethrowCaughtError,
} from 'shared/ReactErrorUtils';
import {createEventListenerWrapperWithPriority} from './ReactDOMEventListener';
import {
removeEventListener,
Expand Down Expand Up @@ -234,14 +230,25 @@ export const nonDelegatedEvents: Set<DOMEventName> = new Set([
...mediaEventTypes,
]);

let hasError: boolean = false;
let caughtError: mixed = null;

function executeDispatch(
event: ReactSyntheticEvent,
listener: Function,
currentTarget: EventTarget,
): void {
const type = event.type || 'unknown-event';
event.currentTarget = currentTarget;
invokeGuardedCallbackAndCatchFirstError(type, listener, undefined, event);
try {
listener(event);
} catch (error) {
if (!hasError) {
hasError = true;
caughtError = error;
} else {
// TODO: Make sure this error gets logged somehow.
}
}
event.currentTarget = null;
}

Expand Down Expand Up @@ -283,7 +290,12 @@ export function processDispatchQueue(
// event system doesn't use pooling.
}
// This would be a good time to rethrow if any of the event handlers threw.
rethrowCaughtError();
if (hasError) {
const error = caughtError;
hasError = false;
caughtError = null;
throw error;
}
}

function dispatchEventsForPlugins(
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4643,7 +4643,7 @@ describe('ReactDOMFizzServer', () => {
await waitForAll([]);
});

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

it('does not invokeGuardedCallback for errors after a preceding fiber suspends', async () => {
it('does not log for errors after a preceding fiber suspends', async () => {
// We can't use the toErrorDev helper here because this is async.
const originalConsoleError = console.error;
const mockError = jest.fn();
Expand Down
24 changes: 17 additions & 7 deletions packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import {
} from 'react-reconciler/src/ReactWorkTags';
import {SyntheticEvent} from 'react-dom-bindings/src/events/SyntheticEvent';
import {ELEMENT_NODE} from 'react-dom-bindings/src/client/HTMLNodeType';
import {
rethrowCaughtError,
invokeGuardedCallbackAndCatchFirstError,
} from 'shared/ReactErrorUtils';
import {enableFloat} from 'shared/ReactFeatureFlags';
import assign from 'shared/assign';
import isArray from 'shared/isArray';
Expand Down Expand Up @@ -354,16 +350,25 @@ function nativeTouchData(x, y) {
// EventPropagator.js, as they deviated from ReactDOM's newer
// implementations.

let hasError: boolean = false;
let caughtError: mixed = null;

/**
* Dispatch the event to the listener.
* @param {SyntheticEvent} event SyntheticEvent to handle
* @param {function} listener Application-level callback
* @param {*} inst Internal component instance
*/
function executeDispatch(event, listener, inst) {
const type = event.type || 'unknown-event';
event.currentTarget = getNodeFromInstance(inst);
invokeGuardedCallbackAndCatchFirstError(type, listener, undefined, event);
try {
listener(event);
} catch (error) {
if (!hasError) {
hasError = true;
caughtError = error;
}
}
event.currentTarget = null;
}

Expand Down Expand Up @@ -619,7 +624,12 @@ function makeSimulator(eventType) {
// do that since we're by-passing it here.
enqueueStateRestore(domNode);
executeDispatchesAndRelease(event);
rethrowCaughtError();
if (hasError) {
const error = caughtError;
hasError = false;
caughtError = null;
throw error;
}
});
restoreStateIfNeeded();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
* @flow
*/

import {rethrowCaughtError} from 'shared/ReactErrorUtils';

import type {ReactSyntheticEvent} from './ReactSyntheticEventType';
import accumulateInto from './accumulateInto';
import forEachAccumulated from './forEachAccumulated';
import {executeDispatchesInOrder} from './EventPluginUtils';
import {executeDispatchesInOrder, rethrowCaughtError} from './EventPluginUtils';

/**
* Internal queue of events that have accumulated their dispatches and are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/

import {invokeGuardedCallbackAndCatchFirstError} from 'shared/ReactErrorUtils';
import isArray from 'shared/isArray';

let hasError = false;
let caughtError = null;

export let getFiberCurrentPropsFromNode = null;
export let getInstanceFromNode = null;
export let getNodeFromInstance = null;
Expand Down Expand Up @@ -62,9 +64,17 @@ function validateEventDispatches(event) {
* @param {*} inst Internal component instance
*/
export function executeDispatch(event, listener, inst) {
const type = event.type || 'unknown-event';
event.currentTarget = getNodeFromInstance(inst);
invokeGuardedCallbackAndCatchFirstError(type, listener, undefined, event);
try {
listener(event);
} catch (error) {
if (!hasError) {
hasError = true;
caughtError = error;
} else {
// TODO: Make sure this error gets logged somehow.
}
}
event.currentTarget = null;
}

Expand Down Expand Up @@ -170,3 +180,12 @@ export function executeDirectDispatch(event) {
export function hasDispatches(event) {
return !!event._dispatchListeners;
}

export function rethrowCaughtError() {
if (hasError) {
const error = caughtError;
hasError = false;
caughtError = null;
throw error;
}
}
130 changes: 0 additions & 130 deletions packages/shared/ReactErrorUtils.js

This file was deleted.

0 comments on commit 944275e

Please sign in to comment.