【问题标题】:Handling Firebase errors in python admin sdk在 python admin sdk 中处理 Firebase 错误
【发布时间】:2018-05-17 04:30:26
【问题描述】:

处理此错误的正确方法是什么?我创建了两个堆栈问题,两个答案都不再起作用。这是我的代码:

from firebase_admin import auth as admin_auth

try:
    uid = 'testUID'
    email = 'test@test.com' # test@test.com already exists
    admin_auth.update_user(uid, email=email)
except Exception as e:
    print(e)

我收到以下回复:

Failed to update user: testUID. Server response: { "error": { "errors": [ { "domain": "global", "reason": "invalid", "message": "EMAIL_EXISTS" } ], "code": 400, "message": "EMAIL_EXISTS" } }

我是否正确地创建了异常?如果我使用:

except ValueError as e:

我得到一个字符串的响应。我想要的是获取状态/代码和消息。

【问题讨论】:

  • 你解决了吗?
  • Nope 刚刚创建了特殊错误。永远无法解析响应

标签: python firebase firebase-authentication


【解决方案1】:

Admin SDK 在 Auth 方法中引发 2 个潜在异常:

  • ValueError 如果参数格式不正确(几乎是您所期望的 ValueError 用于)
  • firebaseadmin.auth.AuthError 如果存在 upstream 故障,这意味着 firebase 出现问题或无法找到、创建用户等。

可以通过查看异常的属性来检查后者。

try:
    auth.get_user(uid)
except auth.AuthError as exc:
    print(exc.code)

显然,ValueError 没有 exc.code。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2020-04-10
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2021-12-21
    • 2019-07-09
    • 1970-01-01
    • 2018-03-20
    相关资源
    最近更新 更多