2

I have an onCall Firebase Cloud Function named getZohoDeskTicketById that throws an error like this:

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.

FunctionsError has a TypeScript warning of '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)

So what is the correct way of narrowing down this error?

2

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, using type predicates seems to be the correct way of arrow to an interface: typescriptlang.org/docs/handbook/2/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.