[英]How to narrow a HttpsError from a Firebase Cloud Function?
I have an onCall
Firebase Cloud Function named getZohoDeskTicketById
that throws an error like this:我有一个名为
getZohoDeskTicketById
的onCall
Firebase Cloud Function 抛出如下错误:
throw new functions.https.HttpsError(
'unknown',
error.response.data.errorCode,
error.response.data
);
And I am calling it like this:我这样称呼它:
import { httpsCallable, FunctionsError } from 'firebase/functions';
// ...
const getZohoDeskTicketById = httpsCallable(functions, 'getZohoDeskTicketById');
const handleClick = async () => {
try {
const result = await getZohoDeskTicketById({
ticketId: '345112301899997',
});
console.log(result);
} catch (error) {
if (error instanceof FunctionsError) { // TypeScript warning here
console.log(error.code); // TypeScript warning here
console.log(error.message); // TypeScript warning here
console.log(error.details); // TypeScript warning here
}
}
};
But I'm having trouble with narrowing down the catch
.但我在缩小范围方面遇到了
catch
。
FunctionsError
has a TypeScript warning of 'FunctionsError' only refers to a type, but is being used as a value here. ts(2693)
FunctionsError
有一个 TypeScript 警告'FunctionsError' only refers to a type, but is being used as a value here. ts(2693)
'FunctionsError' only refers to a type, but is being used as a value here. ts(2693)
and和
error
on each of my console.log's has a TypeScript warning of Object is of type 'unknown'.ts(2571)
我的每个 console.log 上的
error
都有一个 TypeScript 警告Object is of type 'unknown'.ts(2571)
So what is the correct way of narrowing down this error?那么缩小这个错误的正确方法是什么?
I created a user-defined type guard like so:我像这样创建了一个用户定义的类型保护:
function isFunctionsError(error: unknown): error is FunctionsError {
return (error as FunctionsError).details !== undefined;
}
const handleClick = async () => {
try {
const result = await getZohoDeskTicketById({
ticketId: '345112301899997',
});
console.log(result);
} catch (error) {
if (isFunctionsError(error)) {
console.log(error.code);
console.log(error.message);
console.log(error.details);
}
}
};
But if anyone knows of a better way, please post it!但是,如果有人知道更好的方法,请发布!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.