From cbc68ba1496e81fbd293604231eb3c6a555b7a81 Mon Sep 17 00:00:00 2001 From: Tyler Waters Date: Sat, 15 Apr 2017 18:25:22 -0700 Subject: [PATCH] Catch unhandled exceptions during render Fixes #18 --- src/renderer/renderPass.js | 12 ++++++++---- test/tests/renderer/includes/FailureApp.jsx | 12 ++++++++++++ test/tests/renderer/includes/FailureFoo.jsx | 7 +++++++ test/tests/renderer/renderToStaticMarkup.js | 10 ++++++++++ test/tests/renderer/renderToString.js | 10 ++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/tests/renderer/includes/FailureApp.jsx create mode 100644 test/tests/renderer/includes/FailureFoo.jsx diff --git a/src/renderer/renderPass.js b/src/renderer/renderPass.js index ca796b0..b094cd3 100644 --- a/src/renderer/renderPass.js +++ b/src/renderer/renderPass.js @@ -33,10 +33,14 @@ const renderPass = (context, element, staticMarkup = false) => { ) 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) { diff --git a/test/tests/renderer/includes/FailureApp.jsx b/test/tests/renderer/includes/FailureApp.jsx new file mode 100644 index 0000000..d1f1a95 --- /dev/null +++ b/test/tests/renderer/includes/FailureApp.jsx @@ -0,0 +1,12 @@ +import * as React from 'react'; +import Module from '../../../../src/components/Module'; + +const FailureApp = (props) => ( +
+ System.import('./FailureFoo')}> + { module => module ? : null} + +
+); + +export default FailureApp; diff --git a/test/tests/renderer/includes/FailureFoo.jsx b/test/tests/renderer/includes/FailureFoo.jsx new file mode 100644 index 0000000..91eacbd --- /dev/null +++ b/test/tests/renderer/includes/FailureFoo.jsx @@ -0,0 +1,7 @@ +import React, {PureComponent} from 'react' + +export default class FailureFoo extends PureComponent { + render () { + throw new Error('aw snap!') + } +} diff --git a/test/tests/renderer/renderToStaticMarkup.js b/test/tests/renderer/renderToStaticMarkup.js index 8d1c55e..899307a 100644 --- a/test/tests/renderer/renderToStaticMarkup.js +++ b/test/tests/renderer/renderToStaticMarkup.js @@ -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) { @@ -14,4 +15,13 @@ describe('renderToStaticMarkup', () => { done(); }) }); + it('should handle errors properly', () => { + renderToStaticMarkup() + .then(() => { + throw new Error('should not be called') + }) + .catch(e => { + expect(e.message).to.equal('aw snap!') + }) + }) }); diff --git a/test/tests/renderer/renderToString.js b/test/tests/renderer/renderToString.js index 64b448c..cffd884 100644 --- a/test/tests/renderer/renderToString.js +++ b/test/tests/renderer/renderToString.js @@ -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) { @@ -21,4 +22,13 @@ describe('renderToString', () => { }) }) }); + it('should handle errors properly', () => { + renderToString() + .then(() => { + throw new Error('should not be called') + }) + .catch(e => { + expect(e.message).to.equal('aw snap!') + }) + }) });