Skip to content

Commit

Permalink
Add currency error handler for the API
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgodoy-zz committed Jan 4, 2017
1 parent 5022612 commit d152bcc
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions options/optCurrency.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ function optCurrency(command) {
}

request(apiURL, (error, response, body) => {
const apiResponse = JSON.parse(body);
let apiResponse;
try {
apiResponse = JSON.parse(body);
} catch (parseError) {
console.log(chalk.red('Something went wrong in the API. Try in a few minutes'));
return parseError;
}

if ('error' in apiResponse) {
console.log(chalk.red('It was not possible to retrieve what you want'));
return;
return false;
}
console.log(`
return console.log(`
${chalk.yellow('Base currency')} ${getCountryIcon(apiResponse.base)} ${chalk.cyan(apiResponse.base)}
-------------\nCurrency Rates\n
${formatRates(apiResponse.rates)}`);
Expand Down

0 comments on commit d152bcc

Please sign in to comment.