简体   繁体   English

如何缩小来自 Firebase Cloud Function 的 HttpsError?

[英]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:我有一个名为getZohoDeskTicketByIdonCall 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.

相关问题 如何从 Firebase 云 Function 中删除 Firebase 存储文件夹? - How to delete a Firebase Storage folder from a Firebase Cloud Function? 如何从 React stream 聊天应用程序调用 Firebase 云 function - How to call Firebase cloud function from React stream chat app 如何从 Firebase Cloud Function 在 Google Pub/Sub 中发布消息? - How to publish message in Google Pub/Sub from Firebase Cloud Function? 如何从 firebase 云 function 返回数据并返回异步响应? - How to return data from firebase cloud function and return async response? 如何使用 Firebase 从云 function 端查找用户是否已登录? - How to find user is logged in or not from cloud function side using Firebase? 如何从云端访问 firebase 自定义声明 function - How can I access firebase custom claims from a cloud function 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function Firebase 云 Function 适用于 Postman 但不适用于 App - Firebase Cloud Function Works With Postman But Not From App 如何从统一中调用 firebase 示例云 function? - how to call a firebase sample cloud function from unity? 如何从 Firebase Cloud Function 访问 firestore.Timestamp - How to access firestore.Timestamp from Firebase Cloud Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM