首页 > 解决方案 > Firebase HTTP 云函数 - 500 服务器错误,请重试

问题描述

我有一个每秒执行几次的 HTTP 函数(我没有确切的统计数据)。

有时,请求会按预期返回数据,有时会返回以下错误文本:

<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>500 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>The server encountered an error and could not complete your request.<p>Please try again in 30 seconds.</h2>
<h2></h2>
</body></html>

发生这种情况时,我的所有 HTTP 函数都会发生这种情况。5 分钟后提出相同的请求通常会奏效。

现在,我可以想象它是我的代码中的一个错误,但我正在使用中间件来捕获所有错误并以 JSON 响应!

这是我导出的云函数:

const app = express();
app.use(cors({origin: true, credentials: true})); // Allow cross-origin requests
app.get('/', this.reportRequest.bind(this));
app.use(errorMiddleware); // Handle errors

export const report = functions.https.onRequest(app);

这是errorMiddleware

export const errorMiddleware: ErrorRequestHandler = async (e, req, res, next) => {
  const executionId = req.header('function-execution-id');

  const message = 'message' in e ? e.message : e;

  const code = e.code && e.code > 200 && e.code < 600 ? e.code : 500;
  res.status(code).json({message, executionId});

  next();
};

标签: firebaseexpressgoogle-cloud-functions

解决方案


在此处查看并加注星标,以便接收有关此问题状态的更新。

有些人同意你的观点,即使在它到达函数之前,错误就发生在中间件的某个地方,而另一些人则认为请求似乎在它们能够响应之前就已经对实例进行了负载平衡。

一种解决方法是实施指数退避,以识别此错误消息并在逐渐延长的时间延迟后重试。

目前没有修复的 ETA。


推荐阅读