Skip to content

Commit 0db9d71

Browse files
committed
Read current stack from the client current stack during onError
Inside the onError event, the current owner stack is active but it's active to the client runtime but we can access it from server runtime which is what React.captureOwnerStack() points to. So we need to forward this.
1 parent 4a3b5eb commit 0db9d71

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/react-html/src/ReactHTMLServer.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import type {ErrorInfo} from 'react-server/src/ReactFizzServer';
1313

1414
import ReactVersion from 'shared/ReactVersion';
1515

16+
import ReactSharedInternalsServer from 'react-server/src/ReactSharedInternalsServer';
17+
import ReactSharedInternalsClient from 'shared/ReactSharedInternals';
18+
1619
import {
1720
createRequest as createFlightRequest,
1821
startWork as startFlightWork,
@@ -141,7 +144,23 @@ export function renderToMarkup(
141144

142145
const onError = options && options.onError;
143146
if (onError) {
144-
onError(error, errorInfo);
147+
if (__DEV__) {
148+
const prevGetCurrentStackImpl =
149+
ReactSharedInternalsServer.getCurrentStack;
150+
// We're inside a "client" callback from Fizz but we only have access to the
151+
// "server" runtime so to get access to a stack trace within this callback we
152+
// need to override it to get it from the client runtime.
153+
ReactSharedInternalsServer.getCurrentStack =
154+
ReactSharedInternalsClient.getCurrentStack;
155+
try {
156+
onError(error, errorInfo);
157+
} finally {
158+
ReactSharedInternalsServer.getCurrentStack =
159+
prevGetCurrentStackImpl;
160+
}
161+
} else {
162+
onError(error, errorInfo);
163+
}
145164
}
146165
}
147166
const flightRequest = createFlightRequest(

0 commit comments

Comments
 (0)