firebase-admin-node icon indicating copy to clipboard operation
firebase-admin-node copied to clipboard

[Firestore] What is the recommended way to check for an instance of a Firestore error using this package?

Open jketcham opened this issue 2 years ago • 11 comments

Using firebase-admin, what is the recommended way to verify an error is Firestore related? And so that typescript knows that the error instance will have the code property, etc?

Ideally, I'd be able to verify the error is an instance of FirestoreError, but the error I'm experiencing now seems to be grpc related, as the code property is returning a number corresponding to the errors here.

There are several issues in this repository around getting access to error classes, with no clear answers.

If I'm missing something, please point me in the right direction.

jketcham avatar Jul 19 '23 03:07 jketcham

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Jul 19 '23 03:07 google-oss-bot

Thanks for reporting @jketcham . I'll take a look.

ehsannas avatar Jul 25 '23 18:07 ehsannas

Thanks @ehsannas, I appreciate it!

jketcham avatar Jul 25 '23 23:07 jketcham

@jketcham I'm wondering what package is you're using. the Admin Node package doesn't seem to throw any FirestoreErrors. It does throw Errors.

Based on the link you provided, you're probably using the Node.js client, which does define and use FirestoreErrors.

Firestore has several dependencies (e.g. grpc), and it'd be unwise to catch all potential exceptions thrown from dependencies and re-wrap them into a FirestoreError.

ehsannas avatar Jul 27 '23 21:07 ehsannas

@ehsannas I'm using the firebase-admin package in these instances, and so my question here was to see what package I could import some class of an Error from to use in my error handling code to check for specific firestore related error codes. Or if there was some other recommended way to handle firestore-specific errors from firebase-admin.

I think I'm a little confused between the different available packages, specifically, firebase-admin, @google-cloud/firestore and firebase (which includes @firebase/firestore).

Ideally, I would be able to import and check for instances of FirestoreError as you pointed out exists here, but it looks like that isn't exported from firebase-admin or @google-cloud/firestore, only @firebase/firestore included in the firebase package.

So for example I'd like to do something like this:

import { FirestoreError } from "@google-cloud/firestore"; // <--- Or whatever package
import { db } from "./my-firebase-admin-module";

try {
  await db.collection("col_id").doc("doc_id").create({ ... });
} catch (error) {
  if (error instanceof FirestoreError) {
    if (error.code === "already-exists") { // <--- Typescript would pick up on FirestoreErrorCode
      // ...do something specific in this case...
      return;
    }
  }
  // If something unexpected, re-throw
  throw error;
}

What I've seen is when using firebase-admin, I'll get a plain Error back which does have a code property that corresponds to the underlying grpc errors.

jketcham avatar Aug 01 '23 20:08 jketcham

I have the same issue; the only way to work around this it's using the @firebase/util package and importing FirebaseError from it as I saw in other discussions, but this doesn't catch all errors. I'm working with Cloud Messaging and some exceptions are not handled when using instance of.

Would be a nice fix correct export error interfaces to be imported on the projects.

Using:

import { FirebaseError } from 'firebase-admin/lib/utils/error';

Can't use the class because is not exported: Error: Package subpath './lib/utils/error' is not defined by "exports"...


import { FirebaseError } from 'firebase-admin';

Is an interface and can't use with instance of to compare error types.

tiagoboeing avatar Aug 16 '23 12:08 tiagoboeing

To be clear the PR I was opening is mostly for FirebaseAuth where handling different error codes is essential and with the current approach you have to write weird and ugly Typescript type guards to make sure it's indeed a Firebase Auth error.

if ('code' in error && typeof error.code === 'string') {
 ...
}

instead of just

if (error instanceof FirebaseAuthError) {
    if (error.code === "already-exists") { // <--- Typescript would pick up on FirebaseAuthErrorCode
      // ...do something specific in this case...
      return;
    }
  }

IchordeDionysos avatar Oct 13 '23 17:10 IchordeDionysos

hi @jketcham, where you able to resolve this issue? I have the exact same problem. I have a cloud function and would like to do something like this in my function but don't know how to import FirestoreException:

    import * as admin from 'firebase-admin';
    const db = admin.firestore();

    try {
      // if the document is not found, this will throw an exception
      // what is the exception type and how do I read the code 
      await db.doc(`/users/${uid}`).update({
        member,
      });
    } catch (error) {
      if (error instanceof FirestoreException)  { // <==== how do I import this exception??
        if (error.code === 5) { // <=== which API should I use for document not found
           // what should I do over here
        }
      }
    }

ablbol avatar Jan 01 '24 11:01 ablbol

This is the same for all namespaces in firebase-admin. The convenience for users to handle errors is too low. It is not possible to determine whether the error raised is a FirebaseError instance. (Not only FirebaseError but also runtime-level Error exists, so instance checking is necessary.) The simplest approach is for the library to at least provide a dictionary of comparable error codes.

dogemad avatar Apr 12 '24 02:04 dogemad