Error handling for the getExchangeRate function

Now, let's go ahead and look at the next one in our list, that is, the getExchangeRate function. There are actually two things that can go wrong here; the axios request can fail itself and we could also end up with a response that's valid, but the to status code is invalid. In that case, there would be no rate for it.

Now we can go ahead and actually simulate this by commenting out a few lines of code that's going to allow us to test to getExchangeRate in isolation:

const convertCurrencyAlt = async (from, to, amount) => { //const countries = await getCountries(to); const rate = await getExchangeRate(from, to); //const exchangedAmount = amount * rate; // //return `${amount} ${from} is worth ${exchangedAmount} ...

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.