【问题标题】:Firestore error code to human readable messageFirestore 错误代码到人类可读的消息
【发布时间】:2022-07-16 02:04:34
【问题描述】:

我需要一种方法来将 Firestore 在引发 async await 时给我的错误代码转换为人类可读的消息。暴露了哪些接口可以帮助我做到这一点?登录功能示例:

  async onSubmitLogin(e: any) {
    try {
      const user: UserCredential = await signInWithEmailAndPassword(this.auth, e.value["Email"], e.value["Password"]);
    } catch (error: any) {
      // show error popup to user
    }
  }

【问题讨论】:

    标签: angular typescript google-cloud-firestore error-handling


    【解决方案1】:

    我在我的 firestore 类中创建了一个实用函数来执行此操作。错误代码现在终于暴露了,我从这个SO answer 找到的。文档:AuthErrorCodes。每个错误码我都没有看一遍,但是都可以这样处理。

    // Firebase
    import { AuthErrorCodes } from '@angular/fire/auth';
    
    @Injectable({
      providedIn: "root",
    })
    export class FirebaseService {
      private debug: boolean = true;
    
      constructor() {}
    
      public getMessageFromCode(error: string): string | undefined {
        let msg: string | undefined = undefined;
    
        if (error) {
          // convert error to string in case it's not
          error = error.toString();
    
          // get firestore error code
          // e.x.: FirebaseError: Firebase: Error (auth/user-not-found).
          const idx: number = error.indexOf("Firebase: Error ("); // returns -1 when not found
          if (idx != -1) {
            const code: string = error.substring(idx + 17, error.indexOf(")", idx));
            if (this.debug) {
              console.debug(`getMessageFromCode >> code = ${code}`);
            }
    
            // convert code to a human readable message (some messages can be found here: <https://firebase.google.com/docs/auth/admin/errors?hl=en>)
            switch (code) {
              case AuthErrorCodes.INVALID_PASSWORD: {
                msg = "Incorrect password.";
                break;
              }
              case AuthErrorCodes.TOKEN_EXPIRED: {
                msg = "Your token has expired.  Please logout and re-login.";
                break;
              }
              case AuthErrorCodes.USER_CANCELLED: {
                msg = "Login process was stopped by you.";
                break;
              }
              case AuthErrorCodes.USER_DELETED: {
                msg = "This user does not exist.";
                break;
              }
              case AuthErrorCodes.USER_DISABLED: {
                msg = "Your account has been disabled.";
                break;
              }
              case AuthErrorCodes.USER_MISMATCH: {
                msg = "Credential given does not correspond to you.";
                break;
              }
              case AuthErrorCodes.USER_SIGNED_OUT: {
                msg = "You have been signed out.  Please re-sign in.";
                break;
              }
              case AuthErrorCodes.WEAK_PASSWORD: {
                msg = "Your password is too weak.  It must be at least six characters long.";
                break;
              }
              default:
                break;
            }
    
            // log
            if (this.debug) {
              console.debug(`getMessageFromCode >> msg = ${msg}`);
            }
          }
        }
    
        return msg;
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-01
      • 2021-07-07
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      相关资源
      最近更新 更多