-
Notifications
You must be signed in to change notification settings - Fork 27.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
renderToHTML swallowing unhandled exceptions #3452
Comments
As a workaround, you could check Here is how async renderToHTML (req, res, pathname, query) {
try {
const out = await renderToHTML(req, res, pathname, query, this.renderOpts)
return out
} catch (err) {
if (err.code === 'ENOENT') {
res.statusCode = 404
return this.renderErrorToHTML(null, req, res, pathname, query)
} else {
if (!this.quiet) console.error(err)
res.statusCode = 500
return this.renderErrorToHTML(err, req, res, pathname, query)
}
}
} And next.js render method which is recommended when using a custom server. async render (req, res, pathname, query, parsedUrl) {
if (isInternalUrl(req.url)) {
return this.handleRequest(req, res, parsedUrl)
}
if (BLOCKED_PAGES.indexOf(pathname) !== -1) {
return await this.render404(req, res, parsedUrl)
}
const html = await this.renderToHTML(req, res, pathname, query)
if (isResSent(res)) {
return
}
if (this.nextConfig.poweredByHeader) {
res.setHeader('X-Powered-By', 'Next.js ' + process.env.NEXT_VERSION)
}
return sendHTML(req, res, html, req.method, this.renderOpts)
}
|
I met the same, @vjpr have any example to catch the error when using a custom server ? |
Have you encountered such a problem? @vjpr |
We're not planning to change the custom server usage at this point in time as we're working on covering all cases for custom servers without a custom server (which improves performance). |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Expected Behavior
While running in production mode, throwing an unhandled exception during
getInitialProps
should re-throw duringrenderToHTML
Current Behavior
when rendering a route using
renderToHTML
and an unhandled exception is throw ingetInitialProps
,renderToHTML
should re-throw the error.In many cases with a custom server (see SSR Caching example) you want to cache the HTML after it is generated. This is a common use case for
renderToHTML
. However, if an unhandled exception is thrown during that process, the HTML is still returned.desired behavior
Steps to Reproduce (for bugs)
Here is a repository with a barebones recreation. Running under production mode, I would expect this method to catch errors.
https://github.com/bringking/error-example-nextjs/blob/master/server.js#L40
Context
What I was trying to accomplish is any unhandled exception thrown during server render would trigger an express error handler and render the
_error.js
page.Your Environment
The text was updated successfully, but these errors were encountered: