我有一个 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();
};
最佳答案
请查看here并为其加注星标,以便接收有关此问题状态的更新。
有些人同意您的观点,即错误甚至在到达函数之前就发生在中间件中的某个地方,而另一些人则认为请求似乎在实例能够响应之前就已对实例进行了负载平衡。
解决方法是实现指数退避,以识别此错误消息并在逐渐延长的时间延迟后重试。
目前尚无修复的预计时间。
关于Firebase HTTP 云功能 - 500 服务器错误,请重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60975200/