简体   繁体   中英

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. I have one problem though. The function is crashing when the user is trying to register with an already existing email. 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.

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. 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 ".

The error has a code attribute that can be one of the values listed here . In your case, the most appropriate seems to be 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM