如何检查异常是否为 TypeScript 中的 FirebaseError / FirebaseAuthError

huangapple go评论78阅读模式
英文:

How to check if an exception is a FirebaseError / FirebaseAuthError in TypeScript

问题

我正在尝试在我们的服务器上实现Firebase身份验证和用户创建,使用TypeScript进行开发。

我正在创建一个新用户,并将整个创建过程包装在try-catch块中。我试图捕获多种异常,其中之一是由Firebase引发的异常。

问题在于我无法检查异常是由Firebase引发的,还是其他类型的异常。

其他类似问题的答案建议检查异常是否是FirebaseError的实例:

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

    // 其他代码在这里
} catch (error) {
    if(error instanceof FirebaseError){
        // 在这里处理FirebaseError
    }
}

问题是,当createUser函数中出现错误时,错误实例是FirebaseAuthError而不是FirebaseError,所以这个检查失败。

我尝试切换到FirebaseAuthError并导入FirebaseAuthError,但我得到一个错误消息:

包子路径'./lib/utils/error'在'exports'中未被定义
\node_modules\firebase-admin\package.json

那么正确的方式是如何检查捕获的异常是否由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?

答案1

得分: 4

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 completely 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/');
}
英文:

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 completely 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/');
}

huangapple
  • 本文由 发表于 2023年2月14日 20:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定