Skip to content

Thrown error is not an instance of FirebaseError #5841

Closed
@fvilers

Description

@fvilers

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions