【发布时间】:2020-02-08 05:55:59
【问题描述】:
所以我有这样的可调用云函数:
exports.verifiedUserDeactivateTheEvent = functions.https.onCall(async (data, context) => {
const eventID = data.eventID
const verifiedUserID = context.auth.uid
try {
// check user
const verifiedUserDocumentSnapshot = await db.doc(`users/${verifiedUserID}`).get()
if (!verifiedUserDocumentSnapshot.exists || verifiedUserDocumentSnapshot.data().verified === false) {
throw new functions.https.HttpsError('Invalid User', 'Invalid User')
}
// rest of the code
return "success"
} catch(error) {
console.log(error)
return error.message
}
})
如果用户无效或未经授权,则会抛出 httpsError message 'Invalid User'
我在我的 Android 应用程序的可调用函数中使用它
fun verifiedUserDeactivateTheEvent(eventID: String) : Task<String> {
// Create the arguments to the callable function.
val data = hashMapOf(
"eventID" to eventID
)
return functions
.getHttpsCallable("verifiedUserDeactivateTheEvent")
.call(data)
.continueWith { task ->
val result = task.result?.data as String
result
}
}
并像这样使用它
verifiedUserDeactivateTheEvent(selectedEvent.eventID).addOnSuccessListener {
toast(it) // the error message will be shown in here
}.addOnFailureListener {
toast(it.localizedMessage) // not in here
}
我希望错误消息会显示在onFailureListener 上,但该错误字符串消息仍会显示在onSuccessListener 中。如果存在错误,我需要设置不同的操作
如何使可调用函数抛出的https错误显示在onFailureListener中?
【问题讨论】:
标签: android firebase google-cloud-functions