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:
parent
b7bd1f775a
commit
1e4372c627
5 changed files with 23 additions and 2 deletions
|
@ -51,7 +51,8 @@
|
||||||
"packages/next/build/webpack/plugins/terser-webpack-plugin/**",
|
"packages/next/build/webpack/plugins/terser-webpack-plugin/**",
|
||||||
"examples/with-ioc/**",
|
"examples/with-ioc/**",
|
||||||
"examples/with-kea/**",
|
"examples/with-kea/**",
|
||||||
"examples/with-mobx/**"
|
"examples/with-mobx/**",
|
||||||
|
"test/integration/basic/pages/throw-undefined.js"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -307,7 +307,7 @@ export default class Server {
|
||||||
)
|
)
|
||||||
return html
|
return html
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code === 'ENOENT') {
|
if (err && err.code === 'ENOENT') {
|
||||||
res.statusCode = 404
|
res.statusCode = 404
|
||||||
return this.renderErrorToHTML(null, req, res, pathname, query)
|
return this.renderErrorToHTML(null, req, res, pathname, query)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -120,6 +120,10 @@ export default class DevServer extends Server {
|
||||||
return super.renderErrorToHTML(compilationErr, req, res, pathname, query)
|
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 {
|
try {
|
||||||
const out = await super.renderErrorToHTML(err, req, res, pathname, query)
|
const out = await super.renderErrorToHTML(err, req, res, pathname, query)
|
||||||
return out
|
return out
|
||||||
|
|
11
test/integration/basic/pages/throw-undefined.js
Normal file
11
test/integration/basic/pages/throw-undefined.js
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
function ThrowUndefined (props) {
|
||||||
|
return (
|
||||||
|
<div>throw-undefined</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
ThrowUndefined.getInitialProps = () => {
|
||||||
|
throw undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ThrowUndefined
|
|
@ -289,5 +289,10 @@ export default function ({ app }, suiteName, render, fetch) {
|
||||||
expect($('#asPath').text()).toBe('Current asPath: /nav/with-hoc')
|
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...')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue