Override the getCurrentStack temporarily while printing uncaught errors (#30309)
This is just a follow up to #30300. I forgot the uncaught branch.
Sebastian Markbåge committed
Jul 10, 2024 at 16:58 UTC
29552c7907230222acd3f2c586784d24f9da6200
1 file changed
+14
-12
packages/react-reconciler/src/ReactFiberErrorLogger.js
+14
-12
@@ -46,27 +46,29 @@ export function defaultOnUncaughtError(
46
'Consider adding an error boundary to your tree to customize error handling behavior.\n' +
47
'Visit https://react.dev/link/error-boundaries to learn more about error boundaries.';
48
49
- if (enableOwnerStacks) {
50
- console.warn(
51
- '%s\n\n%s\n',
52
- componentNameMessage,
53
- errorBoundaryMessage,
54
- // We let our console.error wrapper add the component stack to the end.
55
- );
56
- } else {
49
+ const prevGetCurrentStack = ReactSharedInternals.getCurrentStack;
50
+ if (!enableOwnerStacks) {
51
// The current Fiber is disconnected at this point which means that console printing
52
// cannot add a component stack since it terminates at the deletion node. This is not
53
// a problem for owner stacks which are not disconnected but for the parent component
54
// stacks we need to use the snapshot we've previously extracted.
55
const componentStack =
56
errorInfo.componentStack != null ? errorInfo.componentStack : '';
63
- // Don't transform to our wrapper
64
- console['warn'](
65
- '%s\n\n%s\n%s',
57
+ ReactSharedInternals.getCurrentStack = function () {
58
+ return componentStack;
59
+ };
60
+ }
61
+ try {
62
+ console.warn(
63
+ '%s\n\n%s\n',
64
componentNameMessage,
65
errorBoundaryMessage,
68
- componentStack,
66
+ // We let our console.error wrapper add the component stack to the end.
67
);
68
+ } finally {
69
+ if (!enableOwnerStacks) {
70
+ ReactSharedInternals.getCurrentStack = prevGetCurrentStack;
71
+ }
72
}
73
}
74
}