Skip to content
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

Thrown error is not an instance of FirebaseError #5841

Closed
fvilers opened this issue Dec 28, 2021 · 2 comments
Closed

Thrown error is not an instance of FirebaseError #5841

fvilers opened this issue Dec 28, 2021 · 2 comments

Comments

@fvilers
Copy link

fvilers commented Dec 28, 2021

Describe your environment

  • Operating System version: Windows 11
  • Browser version: Brave Version 1.33.106 Chromium: 96.0.4664.110 (Official Build) (64-bit)
  • Firebase SDK version: ^9.6.1
  • Firebase Product: firestore

Describe the problem

I'm trying to gracefully catch a "Missing or insufficient permissions." exception when accessing a document that I don't have access to due to specific security rules. The problem is that the SDK throw an error which looks like a FirebaseError but it isn't.

Here's the error in the browser debugger:

image

Even if it looks like a FirebaseError, error instanceof FirebaseError still returns false (see bellow).

I'm currently narrowing the type using the name property and compare with the string FirebaseError but this is not how it should work.

Steps to reproduce:

Try to access a Firestore document but the current rules disallow it. Catch the error and use a TypeScript type guard to narrow its type.

Relevant Code:

try {
  const snapshot = await getDoc(
    doc(getFirestore(), "myCollection", "123-i-dont-have-access").withConverter(
      MY_CONVERTER
    )
  );
  const result = snapshot.exists()
    ? snapshot.data()
    : null;

  setState((s) => ({ ...s, busy: false, result }));
} catch (error) {
  console.log(
    { error },
    "is instance of FirebaseError ?",
    error instanceof FirebaseError
  );
  if (error instanceof FirebaseError) {
    setState((s) => ({ ...s, busy: false, conversation: null }));
  } else {
    setState((s) => ({
      ...s,
      busy: false,
      errorMessage: getErrorMessage(error),
    }));
    console.error(error);
  }
}

The line where I console.log() produces this:

image

@argzdev
Copy link

argzdev commented Dec 28, 2021

Hi @fvilers, thanks for reporting. We'll try to investigate this and see what we can find.

@schmidt-sebastian
Copy link
Contributor

This is a duplicate of #5754, which will be fixed by #5831

@firebase firebase locked and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants