Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Top-level errors
An error thrown from a host container should be "captured" by the host
container itself
  • Loading branch information
acdlite committed Nov 30, 2016
commit 46581a3f8042b0467f318e121838af1863ba1885
1 change: 0 additions & 1 deletion scripts/fiber/tests-failing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ src/renderers/shared/hooks/__tests__/ReactHostOperationHistoryHook-test.js

src/renderers/shared/shared/__tests__/ReactComponent-test.js
* should throw on invalid render targets
* throws usefully when rendering badly-typed elements
* includes owner name in the error about badly-typed elements

src/renderers/shared/shared/__tests__/ReactComponentLifeCycle-test.js
Expand Down
2 changes: 2 additions & 0 deletions scripts/fiber/tests-passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@ src/renderers/shared/shared/__tests__/ReactComponent-test.js
* should support new-style refs with mixed-up owners
* should call refs at the correct time
* fires the callback after a component is rendered
* throws usefully when rendering badly-typed elements

src/renderers/shared/shared/__tests__/ReactComponentLifeCycle-test.js
* should not reuse an instance when it has been unmounted
Expand Down Expand Up @@ -1335,6 +1336,7 @@ src/renderers/shared/shared/__tests__/ReactErrorBoundaries-test.js
* catches errors in componentDidUpdate
* propagates errors inside boundary during componentDidMount
* lets different boundaries catch their own first errors
* discards a bad root if the root component fails

src/renderers/shared/shared/__tests__/ReactIdentity-test.js
* should allow key property to express identity
Expand Down
27 changes: 17 additions & 10 deletions src/renderers/shared/fiber/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,19 +601,26 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
// Search for the nearest error boundary.
let boundary : Fiber | null = null;
if (failedWork) {
let node = failedWork.return;
while (node && !boundary) {
if (node.tag === ClassComponent) {
const instance = node.stateNode;
if (typeof instance.unstable_handleError === 'function') {
// Found an error boundary!
// Host containers are a special case. If the failed work itself is a host
// container, then it acts as its own boundary. In all other cases, we
// ignore the work itself and only search through the parents.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does this happen? If a callback to render throws?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactDOM.render(null, el);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, I'm not sure if we need to bother with this special case much since it happens at the end after all other work so there's no interleaving issue. Maybe for batching across roots but I'm not sure if this solution is sufficient in that case yet anyway.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was added to fix the multiple roots issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (failedWork.tag === HostContainer) {
boundary = failedWork;
} else {
let node = failedWork.return;
while (node && !boundary) {
if (node.tag === ClassComponent) {
const instance = node.stateNode;
if (typeof instance.unstable_handleError === 'function') {
// Found an error boundary!
boundary = node;
}
} else if (node.tag === HostContainer) {
// Treat the root like a no-op error boundary.
boundary = node;
}
} else if (node.tag === HostContainer) {
// Treat the root like a no-op error boundary.
boundary = node;
node = node.return;
}
node = node.return;
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/renderers/shared/shared/__tests__/ReactErrorBoundaries-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,31 @@ describe('ReactErrorBoundaries', () => {
'InnerUpdateBoundary componentWillUnmount',
]);
});

it('discards a bad root if the root component fails', () => {
spyOn(console, 'error');

const X = null;
const Y = undefined;
let err1;
let err2;

try {
let container = document.createElement('div');
ReactDOM.render(<X />, container);
} catch (err) {
err1 = err;
}
try {
let container = document.createElement('div');
ReactDOM.render(<Y />, container);
} catch (err) {
err2 = err;
}

expect(err1.message).toMatch(/got: null/);
expect(err2.message).toMatch(/got: undefined/);
});
}

});