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} ${to}. ${to} can be used in the following`;
};
Now, if I go ahead and mess the USD with QWE, we can save the file and we are going to get another error request failed with status code 422:

What we're going to do again is go through that exact same process. So, we're going to wrap those two lines in a try catch block. Then, if catch runs, you're going to throw a new error using the following format: Unable to get exchange rate for USD and CAD. This is going to be the from USD currency code to CAD currency code. We're going to kick things off by setting up that try catch block. We are going to try to run the following code:
const response = await axios.get(`http://api.fixer.io/latest?base=${from}`);
return response.data.rates[to];
If it runs that would be very nice but, if it doesn't, we do want to handle that as well by throwing a new error. Inside the catch block, we are going to provide our message; it'll just be a template string. Now, the message I want to provide is Unable to get exchange rate for ${from} and ${to}, followed by a period:
const getExchangeRate = async(from, to) =>{
try{
const response = await axios.get(`http://api.fixer.io/latest?base=${from}`);
return response.data.rates[to];
} catch(e){
throw new Error(`Unable to get exchange rate for ${from} and ${to}.`);
}
};
Now we can go ahead and save this and see what happens over inside the browser:

We get Unable to get exchange rate for QWE and MMM. Now currently that's failing because from is invalid, but what if from is valid? What if we're trying to go to USD to MMM? This time around we are actually going to get something different:

Here we just get undefined. It comes up because the return statement in getExchangeRate has response.data.rates. We have a valid from countryCode so valid data comes back, but the to countryCode does not exist, which is where undefined comes from.
We can always go ahead and fix that by creating a variable const rate, and we'll set it equal to response.data.rates[to]. Then, well, a little bit of if logic. If there is rate, we're just going to go ahead and return it. If there's no rate, we're just going to go ahead and throw a new error, which will trigger the catch block, which will print the message to the screen:
const getExchangeRate = async(from, to) =>{
try{
const response = await axios.get(`http://api.fixer.io/latest?base=${from}`);
const rate = response.data.rates[to];
if(rate){
return rate;
} else{
throw new Error();
}
} catch(e){
throw new Error(`Unable to get exchange rate for ${from} and ${to}.`);
}
};
Now, if we save the code using these same codes, we get the message once again: Unable to get the exchange rate for USD and MMM:

Now this message is going to show up if from is invalid, if to is invalid, or if both are invalid.
With that in place, we now have some little error handling set up and we can bring back the rest of the lines from our application. If we run through the app using this bad data here, Unable to get countries that use MMM prints:

Let's switch it back over to valid country codes like USD and CAD. Let's actually use the Euro, EUR, for a change and over inside the browser we should get valid values, since both of those country codes are indeed valid:

Here we get the exchange rate, we get all of the countries that use the Euro, we aren't getting any of our error messages, which is fantastic. So by using regular techniques, things from way back in JavaScript, like try, catch, and throw new error, we're able to create a very nice setup using those async functions.
That is it for this section, and that is it for our little currency-convert example. So, at this point, we've gone through two examples: we went through the app promises example, where we had a contrived set of data; we created a few functions and we got to explore the basics of async/await. Then we went through the currency- convert example, where we used two real APIs and we added a little more robust error handling. At the end of the day, they'll both use the exact same async and await techniques. Hopefully, you're starting to see how this can fit into our Node applications. In the next and final section, we're actually going to use async and await to make some changes to the Node API.