Skip to content

Commit cdfde3a

Browse files
authored
Always rethrow original error when we replay errors (#20425)
We replay errors so you can break on paused exceptions. This is done in the second pass so that the first pass can ignore suspense. Originally this threw the original error. For suppression purposes we copied the flag onto the original error. https://github.com/facebook/react/blob/f1dc626b29b8bf0f14c75a8525e8650b7ea94a47/packages/react-reconciler/src/ReactFiberScheduler.old.js#L367-L369 During this refactor it changed to just throw the retried error: #15151 We're not sure exactly why but it was likely just an optimization or clean up. So we can go back to throwing the original error. That helps in the case where a memoized function is naively not rethrowing each time such as in Node's module system. Unfortunately this doesn't fix the problem fully. Because invokeGuardedCallback captures the error and logs it to the browser. So you still end up seeing the wrong message in the logs. This just fixes so that the error boundary sees the first one.
1 parent b15d6e9 commit cdfde3a

File tree

4 files changed

+4069
-2685
lines changed

4 files changed

+4069
-2685
lines changed

fixtures/dom/src/components/fixtures/error-handling/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ class TrySilenceFatalError extends React.Component {
241241
}
242242
}
243243

244+
function naiveMemoize(fn) {
245+
let memoizedEntry;
246+
return function() {
247+
if (!memoizedEntry) {
248+
memoizedEntry = {result: null};
249+
memoizedEntry.result = fn();
250+
}
251+
return memoizedEntry.result;
252+
};
253+
}
254+
let memoizedFunction = naiveMemoize(function() {
255+
throw new Error('Passed');
256+
});
257+
244258
export default class ErrorHandlingTestCases extends React.Component {
245259
render() {
246260
return (
@@ -288,6 +302,21 @@ export default class ErrorHandlingTestCases extends React.Component {
288302
}}
289303
/>
290304
</TestCase>
305+
<TestCase title="Throwing memoized result" description="">
306+
<TestCase.Steps>
307+
<li>Click the "Trigger error" button</li>
308+
<li>Click the reset button</li>
309+
</TestCase.Steps>
310+
<TestCase.ExpectedResult>
311+
The "Trigger error" button should be replaced with "Captured an
312+
error: Passed". Clicking reset should reset the test case.
313+
</TestCase.ExpectedResult>
314+
<Example
315+
doThrow={() => {
316+
memoizedFunction().value;
317+
}}
318+
/>
319+
</TestCase>
291320
<TestCase
292321
title="Cross-origin errors (development mode only)"
293322
description="">

0 commit comments

Comments
 (0)