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
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:
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):
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:
Then for errors:
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.