Skip to content

Commit

Permalink
Protect against generating stacks failing
Browse files Browse the repository at this point in the history
Errors while generating stacks will bubble to the root. Since this technique
is a bit sketchy, we should probably protect against it.
  • Loading branch information
sebmarkbage committed Apr 11, 2020
1 parent 29bc28a commit c5c2493
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2440,13 +2440,11 @@ describe('ReactErrorBoundaries', () => {
);
});

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

const evilError = {
get message() {
throw new Error('gotta catch em all');
},
message: 'Should catch this',
get stack() {
throw new Error('gotta catch em all');
},
Expand All @@ -2472,7 +2470,7 @@ describe('ReactErrorBoundaries', () => {
);

expect(container.textContent).toContain(
'Caught an error: gotta catch em all.',
'Caught an error: Should catch this.',
);
});
});
18 changes: 11 additions & 7 deletions packages/react-reconciler/src/ReactFiberComponentStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ function describeFiber(fiber: Fiber): string {
}

export function getStackByFiberInDevAndProd(workInProgress: Fiber): string {
let info = '';
let node = workInProgress;
do {
info += describeFiber(node);
node = node.return;
} while (node);
return info;
try {
let info = '';
let node = workInProgress;
do {
info += describeFiber(node);
node = node.return;
} while (node);
return info;
} catch (x) {
return '\nError generating stack: ' + x.message + '\n' + x.stack;
}
}

0 comments on commit c5c2493

Please sign in to comment.