【发布时间】:2026-01-21 01:05:02
【问题描述】:
如果某些先决条件失败(在事务的读取部分),我想向客户端抛出一个 https 错误。另外,如果事务由于意外错误而失败,我想抛出“不可用”错误。
await firestore
.runTransaction(async (transaction) =>
transaction.get(userRef).then((doc) => {
const { totalPoints } = doc.data();
if (totalPoints > 1000) {
...
} else {
throw new functions.https.HttpsError(
"failed-precondition",
"You have less than 1000 points."
);
}
})
)
.catch((err) => {
throw new functions.https.HttpsError(
"unavailable",
"Please, try again later."
);
});
问题是,如果我在 then 中抛出 https 错误,catch 会得到它并抛出另一个 https 错误。
抛出“failed-precondition”错误时如何避免进入catch?
【问题讨论】:
标签: javascript google-cloud-firestore async-await google-cloud-functions try-catch