I am using async and await for the first time, the promises with then if I have used them more, but when using async await I have some doubts when managing the catch.
First of all, I have a main function (FuncionLanzar) that is in charge of calling the rest of the functions that launch a series of requests to an API and process the data.
It has to be a sequential process since I retrieve token, send it to second etc.
This is my main function:
const FuncionLanzar = async() => {
try {
const resp = await postToken()
var access_token = resp.data.access_token;
var token = 'Bearer ' + access_token;
const resp2 = await getStations(token)
var datos = resp2.data;
for (let j = 0; j < datos.length; j++) {
const id_stacions = datos[j].id_station;
const FiwareService = datos[j].Fiware_Service;
const FiwareServicePath = datos[j].Fiware_ServicePath;
const lastdata = await getLastData(token, id_stacions)
var json = lastdata.data.data;
const Jformat_fiware = await FormatFiware(json)
const envio = await PostFiware(Jformat_fiware, id_stacions, FiwareService, FiwareServicePath);
console.log("Envio correcto de los datos");
}
} catch (error) {
console.error('Error en el enviooooooooooo' + error)
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email enviado: ' + info.response);
}
});
}
}
The problem is that it never leads this catch to send a mail when it encounters an error or even if I put a console.log to show the error information. Since in the functions that I call it used axios to make the requests, it also has try async await and when it fails it returns that catch and not the one from the main function.
Function example:
const getStations = async(token) => {
var config = {
headers: { "accept": "application/json", "Authorization": token },
};
try {
return await axios.get(`xxxxxxxxxxxxxxxx`, config)
} catch (error) {
console.error(error)
}
}
I do not want to put the sending of mail in each catch of the functions that are called (getStations), the most logical thing is that the catch of the main function (FuncionLanzar) handles the errors in its catch, but I understand that when finding an error in the function called (getStations) from that error and terminates the process.
How could I do this? removing the try and catch from the functions it called and leaving just the one from the main function?
I don't know if I'm very clear.
Edit01
Hello again, I have tried to eliminate all the try and catch of the functions that are called and in this way if it uses the catch of my main function, I don't know if it is the correct way, but for now it works.
All the best.
Based on the code you posted, I'll explain what you're doing wrong.
Let's say we have the main function that calls the others.
Now let's see what the getStations function does.
As you can see, all the code is wrapped in a
try catch
, so here we have two possibilitiesIn your case what interests you is that in the function
FunctionLanzar
you can process all the exceptions that may occur.I leave you another example that I hope will help you more to understand it.
I leave you the documentation that explains everything very well.
I hope it helps you and that I explained myself well.
Cheers!!!