Next.js getServerSideProps(抛出错误)结果为 404 而不是 500

dus*_*awn 9 server-side-rendering next.js vercel

我有非常基本的设置,即 getServerSideProps 和Vercel生产中的 Sentry 错误日志记录

export const getServerSideProps = async () => {
  // some api call
 if(error) {
  throw new Error("Something went wrong")
}
  return {
    props: {
       data
    }
  };
};
Run Code Online (Sandbox Code Playgroud)

_error.js看起来像这样

import * as Sentry from '@sentry/nextjs'
import { NextPageContext } from 'next'
import NextErrorComponent, { ErrorProps as NextErrorProps } from 'next/error'

const CustomErrorComponent = (props: NextErrorProps) => {
  return <NextErrorComponent statusCode={props.statusCode} />
}

CustomErrorComponent.getInitialProps = async (contextData: NextPageContext) => {
  await Sentry.captureUnderscoreErrorException(contextData)

  
  console.log(contextData.res?.statusCode) // this shows as 404
  console.log(contextData.err?.statusCode) // this as undefined

  return NextErrorComponent.getInitialProps(contextData)
}

export default CustomErrorComponent
Run Code Online (Sandbox Code Playgroud)

请注意,如果我在本地计算机上运行相同的 Prod 版本,我会正确地看到 a,500: 'Internal Server Error'但在 Vercel 上我将其视为404: 'This page could not be found'

如何在 Vercel 上将其显示为 500 错误?

Art*_*hle -1

首先,我没有在 Vercel 部署,也不知道 SEntry(好吧,只是那里文档的红色部分)。因此,这更多的是一般性思维,而不是您所使用的技术堆栈的专业知识。(至少我的Next.js还不错呵呵)

[1] 为什么您对以下的回应不感兴趣:

await Sentry.captureUnderscoreErrorException(contextData)
Run Code Online (Sandbox Code Playgroud)
  • 意味着您同意 Sentry 捕获的错误可能与您返回的错误不同(或者 getInitialProps 正在加载当前的 SEntry 响应吗?也感觉很奇怪)

  • 如果您不需要等待响应,那为什么还要等待呢?你可以直接开火然后忘记(更快)

  • wait 属于 a try{}catch(e){},看起来您的代码可能会抛出意外错误(我假设您在生产运行时不希望出现这种错误。例如,在 404 上,当无法找到 Sentry 时),因此 Sentry 的路径也可能有问题,也许它取决于服务器部署的应用程序中不存在的 ENV。

[2] 您返回的 contextData 可能与您发送到 SEntry 的 contextData 不同,对吗?(当您在等待 SEntry 请求后执行 getInitialProps() 时。

更多想法和发现:

  • 我假设您使用的是 Next.js > 10.0.8 (Sentry 支持最低版本)并且位于 pages/ 目录中,而不是 app/ (显然)

最后(如果这是写了一大堆之后的解决方案,那就太有趣了):

  • SEntry 依赖于Gson (版本很重要!),这最终可能会导致 404 我在某处读过 ^^

您是否手动配置了SEntry?无论如何,确定这里提到的配置(例如setDNS错误可能= 404) https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

如果问题仍然没有解决,请提供配置数据:SEntry(客户端和服务器,剥离秘密),Vercel,Next Config等。ENV,例如SEntry的setDNS是否正确?ETC。