0
votes

I'm using Firebase Cloud Function to create an HTTP function. The purpose of this function is to do a POST and return the response. (I use Axios in order to do the POST)

Here is my code:

exports.doHttpPost = functions.https.onRequest((request, response) => {
    axios.post(url, data, config)
        .then(response => {
            console.log(response);
            response.status(200).send(response);
        })
        .catch(error => {
            console.log(error);
            // --> What should I write here to end the function? <--
        });
});

My question is: How can I end the function if the 'axios.post' fails? I correctly finish the 'then' with 'response.status(200).send(response)'. But I don't know how to finish the 'catch'.

1
In addition to @Teneff answer below, you may watch this official Firebase video: youtube.com/watch?v=7IkUgCLr5oA - Renaud Tarnec

1 Answers

1
votes

Axios provides you response property in the error object. So you should be able to proxy the error response the same way as in the successful flow (not tested):

exports.doHttpPost = functions.https.onRequest((request, response) => {
    axios.post(url, data, config)
        .catch(error => {
            response.status(error.response.status).send(error.response);
        });
});