简体   繁体   English

如何防止 Firebase Cloud Function 崩溃以及如何发送错误消息作为响应?

[英]How to prevent Firebase Cloud Function from crashing and how to send error message as response?

I've created a simple createUser function which is executed on call.我创建了一个简单的createUser function,它在调用时执行。 I have one problem though.我有一个问题。 The function is crashing when the user is trying to register with an already existing email.当用户尝试向已经存在的 email 注册时,function 崩溃。 I mean, it's ok, since no one wants to have 2 users with the same email address but I want to prevent crushing function, instead, I want to send an error message as a response.我的意思是,没关系,因为没有人希望有 2 个用户具有相同的 email 地址,但我想防止破坏 function,相反,我想发送一条错误消息作为响应。

export const createUserTest = functions.https.onCall((data, context) => {
  const {email, password} = data;

  return new Promise((resolve, reject)=>{
    try{
      admin
           .auth()
           .createUser({
             email: email,
             emailVerified: false,
             password: password,
             disabled: false,
           })
           .then((user) => {
             resolve({
                 result: 'success',
                 user: user,
             }) ;
           })
           .catch((error) => {
             reject(error) ;
           });
    }catch(error) {
      reject (error)
    }  
  })  
});

I tried to put the function in to try/catch block but it didn't help.我试图将 function 放入try/catch块,但没有帮助。 Do you have an idea of how I can achieve my goal?你知道我怎样才能实现我的目标吗?

As explained in the doc for Callable Cloud Functions, "to ensure the client gets useful error details, return errors from a callable by throwing (or returning a Promise rejected with) an instance of functions.https.HttpsError ".如可调用云函数的文档中所述,“为确保客户端获得有用的错误详细信息,请通过抛出(或返回被拒绝的 Promise 拒绝) functions.https.HttpsError实例从可调用返回错误。https.HttpsError ”。

The error has a code attribute that can be one of the values listed here .该错误的code属性可以是此处列出的值之一。 In your case, the most appropriate seems to be already-exists .在您的情况下,最合适的似乎是already-exists

On, the other hand, you'll find here the Admin SDK Authentication errors list and you'll see that in case the provided email is already in use by an existing user the error code is auth/email-already-exists .另一方面,您会在此处找到管理员 SDK 身份验证错误列表,并且您会看到如果提供的 email 已被现有用户使用,则错误代码为auth/email-already-exists

So you can adapt your code as follows:因此,您可以按如下方式调整您的代码:

export const createUserTest = functions.https.onCall((data, context) => {
    const { email, password } = data;

    return admin
        .auth()
        .createUser({
            email: email,
            emailVerified: false,
            password: password,
            disabled: false,
        })
        .then((user) => {
            return {
                result: 'success',
                user: user,
            }
        })
        .catch((error) => {
            if (error.code === 'auth/email-already-exists') {
                throw new functions.https.HttpsError('already-exists', 'The provided email is already in use by an existing user');
            } else {
                throw new functions.https.HttpsError('...other code....', '...');
                // If an error other than HttpsError is thrown, your client instead receives an error with the message INTERNAL and the code internal.
            }
        });

});

See here in the doc, how to handle errors on the client side.请参阅文档中的此处,如何在客户端处理错误。 If error.code == 'already-exists' you know that it's because the email is already in use.如果error.code == 'already-exists'你知道这是因为 email 已经在使用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 firebase 云 function 发送自定义响应? - How can I send a custom response from a firebase cloud function? 无法防止节点崩溃并在响应中发送错误 - Unable to prevent node from crashing and to send an error in a response 如何从 firebase 云 function 返回数据并返回异步响应? - How to return data from firebase cloud function and return async response? 如何从 Firebase Cloud Function 在 Google Pub/Sub 中发布消息? - How to publish message in Google Pub/Sub from Firebase Cloud Function? Firebase Cloud功能:错误消息 - Firebase Cloud Function: error message iOS 11-通过Firebase Cloud功能发送Firebase Cloud消息 - iOS 11 - Send Firebase Cloud Message through Firebase Cloud Function Firebase Web 客户端重写了 https.onRequest 函数返回的错误消息,如何防止? - Firebase web client rewrites the error message returned by an https.onRequest function, how to prevent that? Firebase 云 Function 完成状态:“响应错误” - Firebase Cloud Function finished with status: 'response error' 如何从云函数返回 Firebase 时间戳 - How to return a Firebase timestamp from a cloud function 如何从 firebase 云 function 内部获取 firebase 用户 ID - How to get firebase userID from inside a firebase cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM