在 Firebase Cloud 上抛出新函数.https.HttpsError 会拒绝客户端上的内部错误



我已将以下云函数部署到我的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))

我错过了什么吗?

根据您链接的文档,您似乎没有使用此处列出的值作为代码参数。该值必须是 Google API 的规范错误代码之一。例如,"失败身份验证"或"无效电子邮件"未在此处列出。您可以使用"权限拒绝"或"无效参数"代替。

我遇到了同样的问题。这里的新手,所以我的尝试可能是错误的,但我在本地调用函数,我无法到达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')

它现在可以工作了,但我希望有人解释为什么。

我自己的问题只是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')
})

相关内容

最新更新