20

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?

2
  • I have the same case. Did you figured it out? Commented Jun 26, 2018 at 7:32
  • I didn't... Instead of throwing the HttpError when something goes wrong I'm returning an error object. It's a bit inconvenient because you have to do the check on the client but it works Commented Jun 27, 2018 at 11:48

4 Answers 4

18

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This answer helped me solve my problem as well. I think the documentation page has changed though (the link in this answer now brings you to a 404 page). Here is the docs as of today -- firebase.google.com/docs/reference/functions/…
You're right. Thanks for the heads-up. I've updated my answer with that correct link.
It is giving 404 again. Maybe this is the page? firebase.google.com/docs/reference/js/…
@Chris Chiasson, Thx. I've updated the link.
4

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.

Comments

1

I also had the same problem and I realized that the way we were both doing to get a kind of "Dynamic Error Code", from the catch block is wrong.

 .catch((error) => { throw new functions.https.HttpsError(error.code, error.message) }) 

The error param shows where the error is coming from, in this case comes from the authentication part. If you console.log the error code it will appear as "auth/email-already-exists", and this is not one of the canonical values accepted by the HttpsError class.

In this case we should check what is the error code, all possibilities, and then throw the new HttpsError with one of the canonical values accepted by firebase.

Example :

.catch ((error) => { if (error.code === "auth/email-already-exists") { throw new functions.https.HttpsError("already-exists", error.message) } }) 

Comments

0

The problem in my own case was just the region as stated by ctheprinter

The complete working example:

const firebaseFunctions = require('firebase-functions'); const functions = firebaseFunctions.region("europe-west1"); exports.createCredentials = functions.https.onCall((data, context) => { if(!context.auth) throw new firebaseFunctions.https.HttpsError('failed-auth', 'You must be authenticated to call this function') }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.