Rendering of error page from custom express server overwrites custom status code with 404
Bug report
Describe the bug
We use next.js with a custom express server, and we are having problems rendering the error page with a custom status code. It either renders the error page, but with 404 (completely ignoring our 503 status code), or it just displays an unstyled stacktrace.
To Reproduce
- Start with the custom-server-express example
npx create-next-app --example custom-server-express custom-server-express-app - Add the following code just above the line starting with "server.get('*' ..."
server.get('/c', (req, res) => {
console.log("/c")
const err = new Error("Oooh noooo c!!!!")
//console.log(err)
let queryParams = Object.assign(req.query, req.params);
req.status = 503; //Let's add the status code in many different ways, to see if any of them sticks...
req.statusCode = 503;
res.status = 503;
res.statusCode = 503;
err.statusCode = 503;
queryParams = Object.assign(queryParams, { status: 503, statusCode: 503, err: err });
return app.render(req, res, "/_error", queryParams);
})
server.get('/d', (req, res) => {
console.log("/d")
const err = new Error("Oooh noooo d!!!!")
//console.log(err)
let queryParams = Object.assign(req.query, req.params);
req.status = 503;
req.statusCode = 503;
res.status = 503;
res.statusCode = 503;
err.statusCode = 503;
queryParams = Object.assign(queryParams, { status: 503, statusCode: 503, err: err });
return app.renderError(err, req, res, "/d", queryParams);
})
server.get('/e', (req, res) => {
console.log("/e")
const err = new Error("Oooh noooo e!!!!")
//console.log(err)
let queryParams = Object.assign(req.query, req.params);
req.status = 503;
req.statusCode = 503;
res.status = 503;
res.statusCode = 503;
err.statusCode = 503;
queryParams = Object.assign(queryParams, { status: 503, statusCode: 503, err: err });
return app.renderError(err, req, res, "/_error", queryParams);
})
- Start the server
npm install
npm run dev
- Test surfing to
- http://localhost:3000/c
- http://localhost:3000/d
- http://localhost:3000/e
Expected behavior
At least one of these requests should result in a rendered (ie "pretty") error page, that informs about the error 503. And the actual http status code should also be 503.
Actual behavior
-
http://localhost:3000/c
- +Pretty error page
- -Error description says 404 instead of 503
- -Http status code is 404 instead of 503
-
http://localhost:3000/d and http://localhost:3000/e (identical result)
- +Http status code is 503
- -Stacktrace instead of pretty error page
Comments
I know that the documentation mentions that in development mode you get a stacktrace instead of "the pages/_error.js component". But clearly that isn't the case for "/c" above (ie when specifically asking next to render the "/_error" page, but without an error object). So the pretty error rendering is possible, but it should of course respect the provided status code instead of overwriting it with 404.