首页 > 解决方案 > 你能读懂 firebase 函数的 HttpsError 状态吗?

问题描述

我有一个 firebase 函数,在该函数中我手动抛出了一个错误,其状态和消息如下:

throw new functions.https.HttpsError('permission-denied', 'Token does not match');

我从服务器得到的响应是这样的(使用 chrome 开发控制台):

{"error":{"message":"Token does not match","status":"PERMISSION_DENIED"}}

但是当我这样调用函数时,错误中没有状态(使用AngularFire2):

try {
    return await this.functions.httpsCallable<void, void>('testFunc')().toPromise();
} catch (e) {
    console.log(e.name); // "Error"
    console.log(e.message); // "Token does not match"
    console.log(e.status); // undefined
}

if(e.status == 'PERMISSION_DENIED')有什么方法可以获取它的状态,因为我宁愿检查if(e.message == 'Token does not match').

标签: javascriptangularfirebasegoogle-cloud-functionsangularfire2

解决方案


e将是一个HttpsError对象。正如您从 API 文档中看到的那样,它应该有一个名为FunctionsErrorCodecode对象的属性,其中包含作为字符串的 HTTP 状态代码,该字符串应该与您从可调用函数中抛出的内容相匹配。所以我希望成为 string 。e.codepermission-denied


推荐阅读