【问题标题】:How to return error code 'auth/email-already-exists' to client from cloud function?如何从云功能向客户端返回错误代码“auth/email-already-exists”?
【发布时间】:2020-06-21 20:01:07
【问题描述】:

我有一个云功能,我正在执行一些用户注册,但如果电子邮件已被其他用户使用,我无法让它返回错误代码“auth/email-already-exists”。

catch (error) {
    if (error.type === 'UnauthenticatedError') {
        throw new functions.https.HttpsError('unauthenticated', error.message);
    } else if (error.type === 'NotAnAdminError' || error.type === 'InvalidRoleError') {
        throw new functions.https.HttpsError('failed-precondition', error.message);
    } else if (error.code === 'auth/email-already-exists') {          
        //return the error code here
    } else {
         throw new functions.https.HttpsError('internal', error.type); 
    }
}

我已尝试返回 throw new functions.https.HttpsError('email exists', error);,但它一直返回 INTERNAL。我不明白为什么。

【问题讨论】:

    标签: firebase error-handling google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您不能真正将任意字符串值传递给HttpsError() 的第一个参数。有效值列表可在API documentation 中找到。这些字符串直接转换为 HTTP 错误代码。如果您需要特定错误,则必须找到关闭匹配项(可能是“invalid-argument”? - 您必须自己决定),并在其中添加您自己的特定错误消息。

    请注意,仅在 callable function 中抛出 HttpsError 才有意义。由于您没有说明您正在编写什么类型的函数,也没有显示整个源代码,所以我指出这一点以防您不知道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-09
      • 2020-01-22
      • 2021-06-20
      • 2012-08-14
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      相关资源
      最近更新 更多