【问题标题】:Can you read the HttpsError status of a firebase function?你能读懂 firebase 函数的 HttpsError 状态吗?
【发布时间】:2020-09-01 14:36:15
【问题描述】:

我有一个 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')

【问题讨论】:

    标签: javascript angular firebase google-cloud-functions angularfire2


    【解决方案1】:

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

    【讨论】:

    • 我们能看一个如何使用 FunctionsErrorCode 常量(带导入)的示例吗?我在导入和引用中找不到它们。
    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多