你能读懂 firebase 函数的 HttpsError 状态吗?
Can you read the HttpsError status of a firebase function?
我有一个 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')
。
e
将是一个 HttpsError object. As you can see from the API docs, it should have a property called code
which is a FunctionsErrorCode 对象,其中包含作为字符串的 HTTP 状态代码,该字符串应与您从可调用函数中抛出的内容相匹配。所以我希望 e.code
是字符串 permission-denied
.
我有一个 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')
。
e
将是一个 HttpsError object. As you can see from the API docs, it should have a property called code
which is a FunctionsErrorCode 对象,其中包含作为字符串的 HTTP 状态代码,该字符串应与您从可调用函数中抛出的内容相匹配。所以我希望 e.code
是字符串 permission-denied
.