Skip to content

Commit

Permalink
Merge pull request #20 from tswaters/master
Browse files Browse the repository at this point in the history
Catch unhandled exceptions during render
  • Loading branch information
gabrielbull authored Apr 18, 2017
2 parents 40442fa + cbc68ba commit fc957bd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/renderer/renderPass.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ const renderPass = (context, element, staticMarkup = false) => {
</AsyncRenderer>
)
let result;
if (staticMarkup) {
result = renderToStaticMarkup(component);
} else {
result = renderToString(component );
try {
if (staticMarkup) {
result = renderToStaticMarkup(component);
} else {
result = renderToString(component );
}
} catch (e) {
return context.reject(e);
}

if (!context.hasModules && !context.hasStates) {
Expand Down
12 changes: 12 additions & 0 deletions test/tests/renderer/includes/FailureApp.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';
import Module from '../../../../src/components/Module';

const FailureApp = (props) => (
<div>
<Module name="foo" module={() => System.import('./FailureFoo')}>
{ module => module ? <module.default/> : null}
</Module>
</div>
);

export default FailureApp;
7 changes: 7 additions & 0 deletions test/tests/renderer/includes/FailureFoo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, {PureComponent} from 'react'

export default class FailureFoo extends PureComponent {
render () {
throw new Error('aw snap!')
}
}
10 changes: 10 additions & 0 deletions test/tests/renderer/renderToStaticMarkup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { expect } from 'chai';
import renderToStaticMarkup from '../../../src/renderer/renderToStaticMarkup';
import App from './includes/App';
import FailureApp from './includes/FailureApp';

describe('renderToStaticMarkup', () => {
it('should do render to static markup', function (done) {
Expand All @@ -14,4 +15,13 @@ describe('renderToStaticMarkup', () => {
done();
})
});
it('should handle errors properly', () => {
renderToStaticMarkup(<FailureApp/>)
.then(() => {
throw new Error('should not be called')
})
.catch(e => {
expect(e.message).to.equal('aw snap!')
})
})
});
10 changes: 10 additions & 0 deletions test/tests/renderer/renderToString.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { expect } from 'chai';
import renderToString from '../../../src/renderer/renderToString';
import App from './includes/App';
import FailureApp from './includes/FailureApp';

describe('renderToString', () => {
it('should do render to string with module loaded and state fetched', function (done) {
Expand All @@ -21,4 +22,13 @@ describe('renderToString', () => {
})
})
});
it('should handle errors properly', () => {
renderToString(<FailureApp/>)
.then(() => {
throw new Error('should not be called')
})
.catch(e => {
expect(e.message).to.equal('aw snap!')
})
})
});

0 comments on commit fc957bd

Please sign in to comment.