【发布时间】:2021-08-14 14:51:57
【问题描述】:
在 Google Cloud Functions documentation about error reporting 中,它说“......某些类型的未捕获异常(例如异步抛出的异常)将导致在未来的函数调用时发生冷启动。这会增加您的函数将跑起来。”
而在Firebase Documentation about handling errors 中,它说我们应该“通过抛出(或返回被拒绝的 Promise)函数实例来从可调用返回错误。https.HttpsError ...以确保客户端获得有用的错误详细信息”:
if (!(typeof text === 'string') || text.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with one arguments "text" containing the message text to add.');
}
所以在我看来,与其他抛出的 Errors 相比,这听起来像是 HttpsError 受到了某种特殊对待。
我的问题是:如上所示在可调用云函数中抛出HttpsError 会导致函数失败,从而导致冷启动 用于后续调用?
如果是这样的话,抛出 HttpsErrors 将没有意义,因为在这些情况下,像缺少参数这样的“预期”错误,出于性能原因,函数实例可以而且确实应该被重用。
所以如果是这样的话,最好只返回如下内容:
return {
error: true,
error_code: 'invalid_argument',
}
并在客户端而不是 HttpsError 上处理此问题。
或者向客户端返回错误而不导致函数导致冷启动的正确解决方案是返回被拒绝的Promise而不是抛出?
if (!(typeof text === 'string') || text.length === 0) {
// Throwing an HttpsError so that the client gets the error details.
return Promise.reject(new functions.https.HttpsError('invalid-argument', 'The function must be called with one arguments "text" containing the message text to add.'));
}
根据文档,throw new HttpsError(...) 和 return Promise.reject(new HttpsError(...)) 之间似乎没有区别,但也许我读错了。
【问题讨论】:
标签: javascript typescript firebase google-cloud-functions