Matthew Keller
Matthew Keller

Reputation: 463

Using FirestoreErrors in Cloud Functions

How can I use the FirestoreError class in my Firebase cloud functions?

I want to throw errors of type FirestoreError when the document or field I am looking for is undefined in Firestore:

throw new FirestoreError();

When I import FirestoreError like this: import { FirestoreError } from "firebase/firestore"; and throw a FirestoreError, it says that the constructor is private. Can I throw a FirestoreError from a Firebase function or will I have to throw an HttpsError?

Thanks

Upvotes: 0

Views: 83

Answers (1)

Dabbel
Dabbel

Reputation: 2825

FirestoreError is an internal, technical error.

I guess you want to throw an AppError which does relate to your Apps logic and not to a technical problem with Firestore itself.

And for this one may use an HttpsError. Catch it in the client side and check your-app-error-id.

throw functions.https.HttpsError(
  'your-app-error-id',
  'The message.'
);

Docs

To ensure the client gets useful error details, return errors from a callable by throwing ... an instance of functions.https.HttpsError.

If an error other than HttpsError is thrown from your functions, your client instead receives an error with the message INTERNAL and the code internal.

https://firebase.google.com/docs/functions/callable#handle_errors

Example

if (!(typeof text === 'string') || text.length === 0) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
      'one arguments "text" containing the message text to add.');
}

Upvotes: 3

Related Questions