Next.js: getServerSideProps中的异常502错误

创建于 2020-08-07  ·  4评论  ·  资料来源: vercel/next.js

错误报告

描述错误

在无服务器环境中,如果在getServerSideProps中引发异常,则返回的响应状态码为502(错误的网关),绝对不是。 此外,在这种情况下,不会呈现自定义或默认的500页,因此用户可能会看到堆栈跟踪,具体取决于应用程序在生产环境中的部署方式或位置。 这似乎是Next 9.5+的问题,因为它在9.4.x中返回了500个状态代码和一个具有相同代码的正确错误页面。

重现

任何在getServerSideProps中遇到错误的页面都将发生这种情况。 我在这里用“问题”页面创建了一个示例存储库:

https://github.com/spencewood/throw-app

这仅包括:

import React from "react";

export default function Problem() {
  return (
    <>
      <h1>Problem!</h1>
    </>
  );
}

export const getServerSideProps = () => {
  throw new Error("error");
};

部署到Vercel的页面: https ://throw-app.vercel.app/problem

和卷曲:

❯ 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

预期行为

只需将上述应用程序降级为Next 9.4,即可正确呈现Next的默认500页面,并返回500状态代码。 这就是应该发生的情况,因为500表示“服务器错误”,而502表示“无效/无响应”。

屏幕截图

以下是https://throw-app.vercel.app/problem中的屏幕截图,来源在这里: https

下一个9.4

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

下一个9.5

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

其他想法

显然,您不会抛出这样的错误,但是在服务器端生成动态数据以将其传递给应用程序时抛出的任何异常都将导致502。

最有用的评论

同样的问题在这里! 另外,看不到异常日志。

所有4条评论

我想在此处添加更多内容,并提到这不是Vercel问题,即使这是我上面链接的内容。 我已经为Netlify上的问题页面部署了具有完全相同代码的Starter应用程序(尽管它们没有相当好的后备502页面): 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

相应的代码在这里: https :

在Vercel上存在相同的问题-以前,NextJs会使用我们的自定义错误页面显示getServerSideProps的404 /其他问题,但现在我们得到了Vercel 502错误页面。

同样的问题在这里! 另外,看不到异常日志。

我发现它是在https://github.com/vercel/next.js/pull/12841中专门实现的,但不确定确切原因,甚至为什么自定义错误页面的Doc仍然说我们可以依靠500(https://nextjs.org/docs/advanced-features/custom-error-page#500-page)

此页面是否有帮助?
0 / 5 - 0 等级