1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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
This commit is contained in:
Arunoda Susiripala 2018-02-24 18:49:08 +05:30 committed by GitHub
parent 206470283e
commit 77c8677e58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 3 deletions

View file

@ -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
}
}

View file

@ -0,0 +1,5 @@
aa = 10 //eslint-disable-line
export default () => (
<div>Hello</div>
)

View file

@ -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')