Show a better error when someone throws undefined (#6646)

* Show a better error when someone throws undefined

* Update error wording

Co-Authored-By: ijjk <22380829+ijjk@users.noreply.github.com>

* Update error wording in test

Co-Authored-By: ijjk <22380829+ijjk@users.noreply.github.com>

* Update test and add check for statusCode
before updating error
This commit is contained in:
JJ Kasper 2019-03-13 13:39:05 -05:00 committed by GitHub
parent b7bd1f775a
commit 1e4372c627
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 2 deletions

View file

@ -51,7 +51,8 @@
"packages/next/build/webpack/plugins/terser-webpack-plugin/**",
"examples/with-ioc/**",
"examples/with-kea/**",
"examples/with-mobx/**"
"examples/with-mobx/**",
"test/integration/basic/pages/throw-undefined.js"
]
},
"devDependencies": {

View file

@ -307,7 +307,7 @@ export default class Server {
)
return html
} catch (err) {
if (err.code === 'ENOENT') {
if (err && err.code === 'ENOENT') {
res.statusCode = 404
return this.renderErrorToHTML(null, req, res, pathname, query)
} else {

View file

@ -120,6 +120,10 @@ export default class DevServer extends Server {
return super.renderErrorToHTML(compilationErr, req, res, pathname, query)
}
if (!err && res.statusCode === 500) {
err = new Error('An undefined error was thrown sometime during render...')
}
try {
const out = await super.renderErrorToHTML(err, req, res, pathname, query)
return out

View file

@ -0,0 +1,11 @@
function ThrowUndefined (props) {
return (
<div>throw-undefined</div>
)
}
ThrowUndefined.getInitialProps = () => {
throw undefined
}
export default ThrowUndefined

View file

@ -289,5 +289,10 @@ export default function ({ app }, suiteName, render, fetch) {
expect($('#asPath').text()).toBe('Current asPath: /nav/with-hoc')
})
})
it('should show a valid error when undefined is thrown', async () => {
const $ = await get$('/throw-undefined')
expect($('body').text()).toMatch('An undefined error was thrown sometime during render...')
})
})
}