Skip to content

Commit c5c2493

Browse files
committed
Protect against generating stacks failing
Errors while generating stacks will bubble to the root. Since this technique is a bit sketchy, we should probably protect against it.
1 parent 29bc28a commit c5c2493

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

packages/react-dom/src/__tests__/ReactErrorBoundaries-test.internal.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,13 +2440,11 @@ describe('ReactErrorBoundaries', () => {
24402440
);
24412441
});
24422442

2443-
it('should catch errors from errors in the throw phase from errors', () => {
2443+
it('should protect errors from errors in the stack generation', () => {
24442444
const container = document.createElement('div');
24452445

24462446
const evilError = {
2447-
get message() {
2448-
throw new Error('gotta catch em all');
2449-
},
2447+
message: 'Should catch this',
24502448
get stack() {
24512449
throw new Error('gotta catch em all');
24522450
},
@@ -2472,7 +2470,7 @@ describe('ReactErrorBoundaries', () => {
24722470
);
24732471

24742472
expect(container.textContent).toContain(
2475-
'Caught an error: gotta catch em all.',
2473+
'Caught an error: Should catch this.',
24762474
);
24772475
});
24782476
});

packages/react-reconciler/src/ReactFiberComponentStack.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ function describeFiber(fiber: Fiber): string {
5959
}
6060

6161
export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
62-
let info = '';
63-
let node = workInProgress;
64-
do {
65-
info += describeFiber(node);
66-
node = node.return;
67-
} while (node);
68-
return info;
62+
try {
63+
let info = '';
64+
let node = workInProgress;
65+
do {
66+
info += describeFiber(node);
67+
node = node.return;
68+
} while (node);
69+
return info;
70+
} catch (x) {
71+
return '\nError generating stack: ' + x.message + '\n' + x.stack;
72+
}
6973
}

0 commit comments

Comments
 (0)