When an exception is thrown in getServerSideProps in a serverless environment, the response is returned with a status code of 502 (Bad Gateway), which is definitely not what it is. In addition, custom or default 500 pages do not render in this case so the user may see a stack trace depending on how or where the app is deployed in production. This appears to be a Next 9.5+ issue, as it returned a 500 status code and a proper error page with the same code in 9.4.x.
This will happen with any page that encounters an error in getServerSideProps. I've created a sample repo here with a "problem" page:
https://github.com/spencewood/throw-app
This simply includes:
import React from "react";
export default function Problem() {
return (
<>
<h1>Problem!</h1>
</>
);
}
export const getServerSideProps = () => {
throw new Error("error");
};
This page deployed to vercel: https://throw-app.vercel.app/problem
And curl:
❯ curl -I "https://throw-app.vercel.app/problem"
HTTP/2 502
date: Fri, 07 Aug 2020 18:20:24 GMT
content-type: text/plain; charset=utf-8
x-nextjs-page: /problem
content-length: 68
server: Vercel
x-vercel-id: cle1::2cxmx-1596824424449-1d2c09ffce13
strict-transport-security: max-age=63072000; includeSubDomains; preload
cache-control: s-maxage=0
Simply downgrading the above app to Next 9.4 will correctly render the default 500 page for Next and will return a 500 status code. This is what should be happening, as a 500 indicates simply "server error" and 502 indicates "invalid/no response".
Below are screenshots from https://throw-app.vercel.app/problem with source here: https://github.com/spencewood/throw-app
Next 9.4
Next 9.5
Obviously you're not going to throw an error like this, but any exceptions thrown while server-side generating dynamic data to be delivered to the app will cause the 502.
I wanted to add just a little more here and mention that this isn't a Vercel issue even though that's what I linked to above. I've deployed a starter app with the exact same code for the problem page on Netlify (though they don't have a pretty fallback 502 page): https://distracted-curie-3f8cdd.netlify.app/problem
❯ curl -I "https://distracted-curie-3f8cdd.netlify.app/problem"
HTTP/2 502
cache-control: no-cache
date: Fri, 07 Aug 2020 21:16:19 GMT
content-length: 386
content-type: text/plain; charset=utf-8
age: 0
server: Netlify
x-nf-request-id: 17709115-ace0-4300-a026-dc21d6cd5cad-2642676
The code for that is here: https://github.com/spencewood/sonnet-18
Having the same issue with this on Vercel - previously NextJs would use our custom error page to display 404/other issues from getServerSideProps but now we get the Vercel 502 error page.
Same problem here ! Plus, can't see exception logs.
I found that it was specially implemented in https://github.com/vercel/next.js/pull/12841, but not sure got the real reason of that and even why Doc for custom error pages still say that we could rely on 500 (https://nextjs.org/docs/advanced-features/custom-error-page#500-page)
Most helpful comment
Same problem here ! Plus, can't see exception logs.