skip to Main Content

I tried sending the error in every way possible but all i’m getting is "internal" on client side, how can i see the error code so i can display an error message to the user.

FULL CODE:

exports.createUserAndSetClaims = onCall(async (request) => {
  try {
    const userRecord = await getAuth().createUser({
      email: request.data.email,
      emailVerified: false,
      password: request.data.password,
      displayName: request.data.displayName,
      disabled: false,
    });


    await getAuth().setCustomUserClaims(userRecord.uid, {
      admin: request.data.isAdmin,
    });



    return { success: true, user: userRecord };
  } catch (error) {
    console.error("Error in createUserAndSetClaims:", error);
    throw new functions.https.HttpsError(
      error.code,
      error.message 
    );
  }
});

i also tried sending it like this:

throw new functions.https.HttpsError(
    "internal",
    "An error occurred", 
    { code: error.code, message: error.message }
  );

I can see the error.code clearly on cloud functions logs but I can’t read it client side.
I’m using flutter if thats important.

2

Answers


  1. I understand that you want to pass a specific error code when throwing an error in your Callable Cloud Function. As explained in the documentation:

    To ensure the client gets useful error details, return errors from a
    callable by throwing (or for Node.js returning a Promise rejected
    with) an instance of functions.https.HttpsError or
    https_fn.HttpsError.

    The error has a code attribute that can be one of
    the values listed in gRPC Status codes
    . The errors also have a string
    message, which defaults to an empty string. They can also have an
    optional details field with an arbitrary value.

    If an error other than
    an HTTPS error is thrown from your functions, your client instead
    receives an error with the message INTERNAL and the code internal.

    So, in order to get, in the front-end, an error with another error code than the default INTERNAL error code you need to choose one of the possible codes listed here: gRPC Status codes.

    For example (from the doc):

    // Checking attribute.
    if (!(typeof text === "string") || text.length === 0) {
      // Throwing an HttpsError so that the client gets the error details.
      throw new HttpsError("invalid-argument", "The function must be called ...");
    }
    // Checking that the user is authenticated.
    if (!request.auth) {
      // Throwing an HttpsError so that the client gets the error details.
      throw new HttpsError("failed-precondition", "The function must be called while authenticated.");
    }
    
    Login or Signup to reply.
  2. throw new functions.https.HttpsError is used to communicate back to the client nothing more than HTTP status (such as 404 not found, or 500 server error). It’s not meant to carry a payload of data to the application.

    If you have custom data that you want to client to receive, you should simply return that data in an object just like you would for a successful condition. But you should put something in the object that lets the client discern whether or not it was successful.

    For example, on success, you could do exactly as you are now:

        return { success: true, user: userRecord };
    

    Then for errors:

        return { success: false, code: error.code, message: error.message };
    

    It’s up to the client to check the success boolean before using the other attributes.

    You also might want to think carefully about passing along the actual Firebase error values though. They might not be understood by end users. Or they might expose values that should be secured only on the server.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search