From 77c8677e588f52f535ba51f70a887550d7d1790d Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Sat, 24 Feb 2018 18:49:08 +0530 Subject: [PATCH] Fix SSR error handling in the global scope (#3877) * Fix SSR error handling. * Remove unwanted console.logs * Fix a typo. * Fix current tests. * Add a new test case for this case. * Error should only be logged if it is not a 404 --- server/require.js | 10 +++++++++- .../basic/pages/error-in-the-global-scope.js | 5 +++++ .../basic/pages/{error.js => error-inside-page.js} | 0 test/integration/basic/test/rendering.js | 9 +++++++-- 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 test/integration/basic/pages/error-in-the-global-scope.js rename test/integration/basic/pages/{error.js => error-inside-page.js} (100%) diff --git a/server/require.js b/server/require.js index a677f870..c1dd480a 100644 --- a/server/require.js +++ b/server/require.js @@ -55,9 +55,17 @@ export function getPagePath (page, {dir, dist}) { export default function requirePage (page, {dir, dist}) { const pagePath = getPagePath(page, {dir, dist}) + try { return require(pagePath) } catch (err) { - throw pageNotFoundError(page) + if (err.code === 'MODULE_NOT_FOUND') { + throw pageNotFoundError(page) + } + console.error(err) + // If this is not a MODULE_NOT_FOUND error, + // it should be something with the content of the page. + // So, Next.js rendering system will catch it and process. + throw err } } diff --git a/test/integration/basic/pages/error-in-the-global-scope.js b/test/integration/basic/pages/error-in-the-global-scope.js new file mode 100644 index 00000000..d821d830 --- /dev/null +++ b/test/integration/basic/pages/error-in-the-global-scope.js @@ -0,0 +1,5 @@ +aa = 10 //eslint-disable-line + +export default () => ( +
Hello
+) diff --git a/test/integration/basic/pages/error.js b/test/integration/basic/pages/error-inside-page.js similarity index 100% rename from test/integration/basic/pages/error.js rename to test/integration/basic/pages/error-inside-page.js diff --git a/test/integration/basic/test/rendering.js b/test/integration/basic/test/rendering.js index 90cf64a4..830f4882 100644 --- a/test/integration/basic/test/rendering.js +++ b/test/integration/basic/test/rendering.js @@ -96,11 +96,16 @@ export default function ({ app }, suiteName, render, fetch) { expect(pre.text()).toMatch(/The default export is not a React Component/) }) - test('error', async () => { - const $ = await get$('/error') + test('error-inside-page', async () => { + const $ = await get$('/error-inside-page') expect($('pre').text()).toMatch(/This is an expected error/) }) + test('error-in-the-global-scope', async () => { + const $ = await get$('/error-in-the-global-scope') + expect($('pre').text()).toMatch(/aa is not defined/) + }) + test('asPath', async () => { const $ = await get$('/nav/as-path', { aa: 10 }) expect($('.as-path-content').text()).toBe('/nav/as-path?aa=10')