0

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

1 Answer 1

3

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.');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you that makes sense. Is it generally good practice to send an HttpsError for all errors that have to do with application logic or invalid inputs?
@MatthewKeller Yes, see the updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.