Next.js: 502 error on exception in getServerSideProps

Created on 7 Aug 2020  ·  4Comments  ·  Source: vercel/next.js

Bug report

Describe the bug

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.

To Reproduce

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

Expected behavior

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".

Screenshots

Below are screenshots from https://throw-app.vercel.app/problem with source here: https://github.com/spencewood/throw-app

Next 9.4

Screen Shot 2020-08-07 at 1 25 55 PM

Next 9.5

Screen Shot 2020-08-07 at 1 25 59 PM

Other Thoughts

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.

bug

Most helpful comment

Same problem here ! Plus, can't see exception logs.

All 4 comments

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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jesselee34 picture jesselee34  ·  3Comments

swrdfish picture swrdfish  ·  3Comments

sospedra picture sospedra  ·  3Comments

timneutkens picture timneutkens  ·  3Comments

olifante picture olifante  ·  3Comments