How to decipher between different Firebase errors in single Cloud Function catch block?

Viewed 32

I have a Firebase Cloud Function that performs two async tasks (create user by Authentication and batch write by Firestore) with a single catch block for all errors. My question is how can I decipher if the error is thrown from Authentication or from Firestore in the catch block before I throw the HTTPS error to the client?

exports.createUser = functions.https.onCall((data, _context) => {
    const email = data.email;
    const password = data.password;
    const birthday = data.birthday;
    const name = data.name;

    return admin.auth().createUser({
        email: email,
        password: password,
    })
    .then((userRecord) => {
        const userId = userRecord.uid;
        const db = admin.firestore();
        const batch = db.batch();
        const testDocRef1 = db.collection("test1").doc(userId);
        const testDocRef2 = db.collection("test2").doc(userId);

        batch.create(testDocRef1, {name: name, email: email});
        batch.create(testDocRef2, {name: name, birthday: birthday});

        return batch.commit().then(() => {
            return Promise.resolve({"userId": userId});
        });
    })
    .catch((error) => {
        // how can I decipher which async task this error came from?
        throw new functions.https.HttpsError("unknown", "Refer to details for error specifics.", error);
    });
});
0 Answers
Related