[英]Throwing new functions.https.HttpsError on Firebase Cloud Function rejects as Internal Error on Client
我將以下 Cloud Function 部署到我的 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
而不是在客戶端捕獲的錯誤作為文檔狀態時,我都會得到一個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 的規范錯誤代碼之一。 例如,“failed-auth”或“invalid-email”未在此處列出。 您可以改用“permission-denied”或“invalid-argument”。
我遇到過同樣的問題。 這里是新手,所以我的嘗試可能是錯誤的,但我在本地調用了這些函數,但無法訪問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')
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.