抛出新函数。Firebase 云函数上的 https.HttpsError 被拒绝为客户端上的内部错误

问题描述 投票:0回答:4

我已将以下云功能部署到我的 Firebase 项目中:

exports.createCredentials = functions.https.onCall((data, context) => {
    if(!context.auth)
        throw new functions.https.HttpsError('failed-auth', 'You must be authenticated to call this function')

    const { email, password } = data;

    if(!(typeof email === 'string'))
        throw new functions.https.HttpsError('invalid-email', 'Email must be a string')
    if(!(typeof password === 'string'))
        throw new functions.https.HttpsError('invalid-password', 'Password must be a string')

    return admin.auth().createUser({
        email,
        password
    })
    .then(userRecord => {
        return { userRecord }
    })
    .catch((error) => { 
        throw new functions.https.HttpsError(error.code, error.message)
    })
})

问题是,每当我抛出一个新的

functions.https.HttpsError
而不是在客户端中以 docs 状态捕获错误时,我都会收到
internal
错误代码。在我的函数日志中,它显示了我尝试调用 HttpsError 时出现的未处理错误。这是日志的示例:

Unhandled error Error: Unknown error status: auth/email-already-exists
    at new HttpsError (/user_code/node_modules/firebase-functions/lib/providers/https.js:80:19)
    at admin.auth.createUser.then.catch (/user_code/index.js:26:9)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我知道,如果没有抛出 HttpsError,这种类型的函数会拒绝内部错误,但据我所知,我的函数中并非如此。我正在 React 中对前端进行编程,所以我是这样调用这个 firebase 函数的:

let options = {
    email: arquitect.email,
    password: arquitect.password
}

let createCredentials = firebase.functions().httpsCallable('createCredentials')
createCredentials(options)
    .then(result => {

    })
    .catch(error => console.log(error))

我有什么遗漏的吗?

javascript reactjs firebase google-cloud-functions
4个回答
17
投票

您似乎没有按照您链接的文档使用代码参数的值在此处列出。该值必须是 Google API 的规范错误代码之一。例如,“失败的身份验证”或“无效的电子邮件”未在那里列出。您可以使用“permission-denied”或“invalid-argument”来代替。


4
投票

我也有同样的问题。这里是新手,所以我的尝试可能是错误的,但我在本地调用函数,但无法访问

HttpsError

因此,在执行以下操作时:

const functionsGlobal = require('firebase-functions')
const functions = functionsGlobal.region('europe-west2')

我必须像这样得到

HttpsError

throw new functionsGlobal.https.HttpsError('failed-precondition',
    'The function must be called while authenticated.', 'hello')

代替:

throw new functions.https.HttpsError('failed-precondition', 
    'The function must be called while authenticated.', 'hello')

现在可以使用,但我希望有人能解释原因。


0
投票

我自己的情况的问题只是 ctheprinter 指定的区域

完整的工作示例:

const firebaseFunctions = require('firebase-functions');
const functions = firebaseFunctions.region("europe-west1");

exports.createCredentials = functions.https.onCall((data, context) => {

    if(!context.auth)
        throw new firebaseFunctions.https.HttpsError('failed-auth', 'You must be authenticated to call this function')

})

0
投票

我也遇到了同样的问题,我意识到我们从 catch 块获取一种“动态错误代码”的方式是错误的。

   .catch((error) => { 
    throw new functions.https.HttpsError(error.code, error.message)
})

错误参数显示错误来自何处,在本例中来自身份验证部分。如果您 console.log 错误代码,它将显示为“auth/email-already-exists”,这不是 HttpsError 类接受的规范值之一。

在这种情况下,我们应该检查错误代码是什么,所有可能性,然后使用 firebase 接受的规范值之一抛出新的 HttpsError。

示例:

.catch ((error) => { 
  if (error.code === "auth/email-already-exists") {
    throw new functions.https.HttpsError("already-exists", error.message)
  }
})

 
© www.soinside.com 2019 - 2024. All rights reserved.