From 493035b612f83aa782ff47e206db7a608bc72745 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 27 Mar 2024 23:29:52 -0400 Subject: [PATCH] Don't log onRecoverableError if the current commit fail We didn't recover after all. --- .../src/__tests__/ReactDOMFizzServer-test.js | 18 +++++++++--------- .../react-reconciler/src/ReactFiberWorkLoop.js | 9 ++++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js index 229f72e34fb4b..2bb8c7f3dfe7e 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js @@ -6004,23 +6004,23 @@ describe('ReactDOMFizzServer', () => { expect(reportedServerErrors.length).toBe(1); expect(reportedServerErrors[0].message).toBe('Oops!'); + const reportedCaughtErrors = []; const reportedClientErrors = []; ReactDOMClient.hydrateRoot(container, , { + onCaughtError(error) { + reportedCaughtErrors.push(error); + }, onRecoverableError(error) { reportedClientErrors.push(error); }, }); await waitForAll([]); expect(getVisibleChildren(container)).toEqual('Oops!'); - expect(reportedClientErrors.length).toBe(1); - if (__DEV__) { - expect(reportedClientErrors[0].message).toBe('Oops!'); - } else { - expect(reportedClientErrors[0].message).toBe( - 'The server could not finish this Suspense boundary, likely due to ' + - 'an error during server rendering. Switched to client rendering.', - ); - } + // Because this is rethrown on the client, it is not a recoverable error. + expect(reportedClientErrors.length).toBe(0); + // It is caught by the error boundary. + expect(reportedCaughtErrors.length).toBe(1); + expect(reportedCaughtErrors[0].message).toBe('Oops!'); }); it("use a promise that's already been instrumented and resolved", async () => { diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.js b/packages/react-reconciler/src/ReactFiberWorkLoop.js index 1d7a8e99ddf60..29d56c56ddefc 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.js @@ -1099,7 +1099,14 @@ function finishConcurrentRender( // Commit the placeholder. break; } - case RootErrored: + case RootErrored: { + // This render errored. Ignore any recoverable errors because we weren't actually + // able to recover. Instead, whatever the final errors were is the ones we log. + // This ensures that we only log the actual client side error if it's just a plain + // error thrown from a component on the server and the client. + workInProgressRootRecoverableErrors = null; + break; + } case RootSuspended: case RootCompleted: { break;