firebase - 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();
};
解决方案
请在此处查看并加注星标,以便接收有关此问题状态的更新。
有些人同意你的观点,即使在它到达函数之前,错误就发生在中间件的某个地方,而另一些人则认为请求似乎在它们能够响应之前就已经对实例进行了负载平衡。
一种解决方法是实施指数退避,以识别此错误消息并在逐渐延长的时间延迟后重试。
目前没有修复的 ETA。
推荐阅读
- php - 在 laravel 中的模型上使用 current() 函数
- c++ - C ++如何使用用户定义的比较器将priority_queue作为属性正确添加到类中?
- regex - 如何添加多个前缀
- python - 如何使用 pre_save 信号检查是否在 django admin 中选择了新文件?
- android - 您能告诉我以下订阅示例有什么问题吗?
- java - 如何调用片段并一起发送数据?
- glsl - 渲染有时需要不同着色器配置的多种不同材质
- reactjs - React 不能使用 import 作为组件
- ios - 在没有 IBOutlets 的情况下以编程方式更新布局约束 - Swift
- python - 将 conda 与 atom 结合使用(或不使用)的推理