Skip to content

Commit

Permalink
Basic SSR support for error boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jimfb authored and jim committed May 4, 2016
1 parent c9504d9 commit d9620e1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/core/__tests__/ReactErrorBoundaries-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

var React;
var ReactDOM;
var ReactDOMServer;

describe('ReactErrorBoundaries', function() {

beforeEach(function() {
ReactDOM = require('ReactDOM');
ReactDOMServer = require('ReactDOMServer');
React = require('React');
});

Expand Down Expand Up @@ -55,6 +57,41 @@ describe('ReactErrorBoundaries', function() {
expect(EventPluginHub.putListener).not.toBeCalled();
});

it('renders an error state (ssr)', function() {
class Angry extends React.Component {
render() {
throw new Error('Please, do not render me.');
}
}

class Boundary extends React.Component {
constructor(props) {
super(props);
this.state = {error: false};
}
render() {
if (!this.state.error) {
return (<div><button onClick={this.onClick}>ClickMe</button><Angry /></div>);
} else {
return (<div>Happy Birthday!</div>);
}
}
onClick() {
/* do nothing */
}
unstable_handleError() {
this.setState({error: true});
}
}

var EventPluginHub = require('EventPluginHub');
var container = document.createElement('div');
EventPluginHub.putListener = jest.fn();
container.innerHTML = ReactDOMServer.renderToString(<Boundary />);
expect(container.firstChild.innerHTML).toBe('Happy Birthday!');
expect(EventPluginHub.putListener).not.toBeCalled();
});

it('will catch exceptions in componentWillUnmount', function() {
class ErrorBoundary extends React.Component {
constructor() {
Expand Down
16 changes: 12 additions & 4 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,20 +413,28 @@ var ReactCompositeComponentMixin = {
context
) {
var markup;
var checkpoint = transaction.checkpoint();
var canCheckpoint = !!transaction.checkpoint;
var checkpoint = canCheckpoint ? transaction.checkpoint() : null;
try {
markup = this.performInitialMount(renderedElement, nativeParent, nativeContainerInfo, transaction, context);
} catch (e) {
// Roll back to checkpoint, handle error (which may add items to the transaction), and take a new checkpoint
transaction.rollback(checkpoint);
if (canCheckpoint) {
transaction.rollback(checkpoint);
}
this._instance.unstable_handleError(e);
if (this._pendingStateQueue) {
this._instance.state = this._processPendingState(this._instance.props, this._instance.context);
}
checkpoint = transaction.checkpoint();
if (canCheckpoint) {
checkpoint = transaction.checkpoint();
}

this._renderedComponent.unmountComponent(true);
transaction.rollback(checkpoint);

if (canCheckpoint) {
transaction.rollback(checkpoint);
}

// Try again - we've informed the component about the error, so they can render an error message this time.
// If this throws again, the error will bubble up (and can be caught by a higher error boundary).
Expand Down

0 comments on commit d9620e1

Please sign in to comment.