admin管理员组

文章数量:1401958

I'm trying to implement Firebase authentication and user creation on our server, working on TypeScript.

I'm creating a new user, and I have wrapped the whole creation inside a try-catch block. I'm trying to catch several exceptions, one of them being exception thrown by Firebase.

The problem is that I can't check if the exception was thrown by the Firebase, or if it's another type of exception.

Other answers to similar kind of questions suggest to check if the exception is an instance of FirebaseError:

let firebaseUser;
try {
    firebaseUser = await firebase.auth().createUser({
        email: email,
        password: password,
    });

    // Other code here
} catch (error) {
    if(error instanceof FirebaseError){
        // handle FirebaseError here
    }
}

The problem is, that when there is an error in the createUser function, the error is an instance of FirebaseAuthError, not FirebaseError, so this check fails.

I tried to switch it to FirebaseAuthError and import the FirebaseAuthError as well, but I get an error message:

Package subpath './lib/utils/error' is not defined by "exports" in \node_modules\firebase-admin\package.json

So what would be the correct way to check that the caught exception is thrown by FirebaseAuth?

I'm trying to implement Firebase authentication and user creation on our server, working on TypeScript.

I'm creating a new user, and I have wrapped the whole creation inside a try-catch block. I'm trying to catch several exceptions, one of them being exception thrown by Firebase.

The problem is that I can't check if the exception was thrown by the Firebase, or if it's another type of exception.

Other answers to similar kind of questions suggest to check if the exception is an instance of FirebaseError:

let firebaseUser;
try {
    firebaseUser = await firebase.auth().createUser({
        email: email,
        password: password,
    });

    // Other code here
} catch (error) {
    if(error instanceof FirebaseError){
        // handle FirebaseError here
    }
}

The problem is, that when there is an error in the createUser function, the error is an instance of FirebaseAuthError, not FirebaseError, so this check fails.

I tried to switch it to FirebaseAuthError and import the FirebaseAuthError as well, but I get an error message:

Package subpath './lib/utils/error' is not defined by "exports" in \node_modules\firebase-admin\package.json

So what would be the correct way to check that the caught exception is thrown by FirebaseAuth?

Share Improve this question edited Feb 14, 2023 at 12:29 Lappesson asked Feb 14, 2023 at 12:25 LappessonLappesson 1131 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I think the best thing you can do, is writing a type guard to ensure your error is a FirebaseAuthError that you will import using import type. I checked, and it seems that it's because the library doesn't export it.

You can freely benefit from the type used by firebase, however, as the module is not listed in the exports, you won't be able to use it at runtime. the import type syntax allows you to still use it during development, but it will be pletely ignored at build time. That means you can't use instanceof FirebaseAuthError as only the type is imported, and you can't use instanceof on something else than a class.

I'm not sure about the following statement, but I guess that every error prefixed by auth/ seem to be a FirebaseAuthError.

Thus, you could write a type guard as follows:

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


function isFirebaseAuthError(error: FirebaseError): error is FirebaseAuthError {
  return error.code.startsWith('auth/');
}

本文标签: javascriptHow to check if an exception is a FirebaseErrorFirebaseAuthError in TypeScriptStack Overflow