-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Firestore] What is the recommended way to check for an instance of a Firestore error using this package? #2257
Comments
I found a few problems with this issue:
|
Thanks for reporting @jketcham . I'll take a look. |
Thanks @ehsannas, I appreciate it! |
@jketcham I'm wondering what package is you're using. Based on the link you provided, you're probably using the Node.js client, which does define and use 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 |
@ehsannas I'm using the I think I'm a little confused between the different available packages, specifically, Ideally, I would be able to import and check for instances of 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 |
I have the same issue; the only way to work around this it's using the 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: import { FirebaseError } from 'firebase-admin'; Is an interface and can't use with |
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;
}
} |
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:
|
This is the same for all namespaces in |
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 thecode
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 thecode
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.
The text was updated successfully, but these errors were encountered: