简体   繁体   中英

Firebase web client rewrites the error message returned by an https.onRequest function, how to prevent that?

I can not retrieve the message I send back from an "https.onRequest" function. The firebase client rewrites the message based on the error code and I can not recover the originally sent body or JSON.

On Firebase Functions :

exports.auth = functions.https.onRequest( (request, response) => {
    cors(request, response, async () => {
        try {
            return response.status(200).json({
                    data: "Hello"
                });

        } catch (error) {
            return response
                .status(403)
                .send('User not activated')
        }
    });
});

All is ok if I analyze in the chrome development console the return of the request: Body (response.send) : "User not activated"

But on the front side

auth: function (email, password) {
    var register = firebaseFunctions.httpsCallable('auth');

    return register({
            email: email,
            password: password
        })
        .then(async (result) => {
            // ...
        })
        .catch((err) => {
            throw err
        });
}

In my catch statement :

err.message > permission-denied
err.details > undefined
err.code > 403

I do not know how to recover the message that I sent "User not activated" and that would allow me to process a 403 error code more accurately: the user can not connect because it is not activated.

Thanks a lot !

You're mixing up callable and regular HTTP type functions. That's not going to work at all.

On your function side, you're declaring the function as a regular HTTP type function using the onRequest callback. This means you need to use an HTTP client library to access it.

On your client side, you're trying to invoke the function as a callable type function using the Functions client SDK. This requires that you declare your function using an onCall callback, not the onRequest callback that you're using now. Callables have a special protocol that's implemented by the client SDK.

The client and server have to match; right now they do not. If you want to use a callable function, follow all the instructions in the linked documentation. If you want a regular HTTP type function, you can't use Functions client SDK. You have to use some other HTTP client library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM