Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing new functions.https.HttpsError on Firebase Cloud Function rejects as Internal Error on Client

I have the following Cloud Function deployed to my Firebase Project:

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)
    })
})

The problem is that whenever I throw a new functions.https.HttpsError instead of the error being catched in the client as the docs state, I get an internal error code. In my function's log, it shows an unhandled error where I tried calling the HttpsError. Here's an example of the log:

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)

I know that this type of function rejects an internal error if no HttpsError was thrown, but as far as I know that is not the case in my function. I am programming my front end in React so here's how I call this firebase function:

let options = {
    email: arquitect.email,
    password: arquitect.password
}

let createCredentials = firebase.functions().httpsCallable('createCredentials')
createCredentials(options)
    .then(result => {

    })
    .catch(error => console.log(error))

Is there something I am missing?

like image 276
Gabriel Schneider Avatar asked Jun 19 '18 20:06

Gabriel Schneider


2 Answers

It appears you aren't using the values listed here for the code parameter, as per the doc you linked. The value must be one of the canonical error codes for Google APIs. "failed-auth" or "invalid-email" for example aren't listed there. You may use "permission-denied" or "invalid-argument" instead.

like image 152
LundinCast Avatar answered Nov 16 '22 23:11

LundinCast


I had the same issue. Novice here, so my attempt may be wrong but I was calling the functions locally and I couldn't get to the HttpsError.

So whilst doing the following:

const functionsGlobal = require('firebase-functions')
const functions = functionsGlobal.region('europe-west2')

I had to then get the HttpsError like this:

throw new functionsGlobal.https.HttpsError('failed-precondition',
    'The function must be called while authenticated.', 'hello')

Instead of:

throw new functions.https.HttpsError('failed-precondition', 
    'The function must be called while authenticated.', 'hello')

It works now but I would love for someone to explain why.

like image 32
ctheprinter Avatar answered Nov 17 '22 00:11

ctheprinter