我在这里读到未处理的错误可能会导致冷启动。我正在实现一个触发功能,如下:
exports.detectPostLanguage = functions
.region("us-central1")
.runWith({ memory: "2GB", timeoutSeconds: "540" })
.firestore.document("posts/{userId}/userPosts/{postId}")
.onCreate(async (snap, context) => {
// ...
const language = await google.detectLanguage(post.text);
// ... more stuff if no error with the async operation
});
我是否需要捕获 google.detectLanguage() 方法错误以避免冷启动?
如果是,我应该怎么做(A 或 B)?
A:
const language = await google.detectLanguage(post.text)
.catch(err => {
functions.logger.error(err);
throw err; // <---- Will still cause the cold start?
});
乙:
try {
var language = await google.detectLanguage(post.text)
} catch(err) {
functions.logger.error(err);
return null; // <----
}
更新
基于弗兰克解决方案:
exports.detectPostLanguage = functions
.region("us-central1")
.runWith({ memory: "2GB", timeoutSeconds: "540" })
.firestore.document("posts/{userId}/userPosts/{postId}")
.onCreate(async (snap, context) => {
try {
// ...
const language = await google.detectLanguage(post.text);
// ... more stuff if no error with the async operation
} catch(err) {
return Promise.reject(err);
}
return null; // or return Promise.resolve();
});