-
Notifications
You must be signed in to change notification settings - Fork 373
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 |
(Speaking from the perspective of auth errors here, as dogemd said this is the case everywhere though) Any update on this? Currently error handing is really awkward. In Typescript all catch blocks give } catch(error) {
if (error instanceof Error){
console.log(error.message) // proper types for message here
}
} Because
(also IMO, "errorInfo" storing a I found a hack but its quite ugly: type FirebaseErrorParent = {
errorInfo: FirebaseError;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isFirebaseError(err: any): err is FirebaseErrorParent {
return (
typeof err === "object" &&
"errorInfo" in err &&
"codePrefix" in err &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
typeof err.errorInfo === "object" &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
"code" in err.errorInfo &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
"message" in err.errorInfo
);
} } catch (err) {
if (isFirebaseError(err) && err.errorInfo.code !== "auth/user-not-found") { //here we have proper types now
//
}
} Without this you need to do some weird typecasting, or ignoring the eslint rule which is also not ideal. (also it would be nice if |
This is so weird because it makes almost impossible to write an unit test targeting some behavior regard Firebase errors, I'm aware of the "hacks" around it, but it makes no sense to me. It's easier to not even check for instances of Firebase errors in first place. They should expose a better interface for constructing the errors in order to make it easier to create an instance in tests. |
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: