Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle offline error #71

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"cli-table3": "^0.5.1",
"cli-welcome": "^1.4.0",
"comma-number": "^2.0.1",
"is-online": "^8.2.1",
"lodash.orderby": "^4.6.0",
"log-symbols": "^3.0.0",
"meow": "^6.1.0",
Expand Down
31 changes: 31 additions & 0 deletions utils/fetchAndHandleErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const to = require('await-to-js').default;
const axios = require('axios');
const isOnline = require('is-online');
const handleError = require('cli-handle-error');

/**
* Handles errors and return Promise object with `error and `response`
* @param {string} url URL to fetch from
*/

module.exports = async url => {
const [[error, response], online] = await Promise.all([
to(axios.get(url)),
isOnline({ timeout: 3000 })
]);

//Handle offline error
if (!online)
handleError(
` Looks like you're offline.`,
new Error('offline'),
false,
true
);

//Handle every other error!
if (online && error)
handleError(`API is down, try again later.`, error, false, true);

return { error, response };
};
16 changes: 5 additions & 11 deletions utils/getCountries.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const axios = require('axios');
const chalk = require('chalk');
const cyan = chalk.cyan;
const dim = chalk.dim;
const comma = require('comma-number');
const { sortingKeys } = require('./table.js');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const orderBy = require('lodash.orderby');
const fetchAndHandleErrors = require('./fetchAndHandleErrors.js');

module.exports = async (
spinner,
Expand All @@ -16,22 +14,18 @@ module.exports = async (
{ sortBy, limit, reverse }
) => {
if (!countryName && !states) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/countries`)
const { response } = await fetchAndHandleErrors(
`https://corona.lmao.ninja/countries`
);
handleError(`API is down, try again later.`, err, false);

let allCountries = response.data;

// Limit.
allCountries = allCountries.slice(0, limit);

// Sort & reverse.
const direction = reverse ? 'asc' : 'desc';
allCountries = orderBy(
allCountries,
[sortingKeys[sortBy]],
[direction]
);
allCountries = orderBy(allCountries, [sortingKeys[sortBy]], [direction]);

// Push selected data.
allCountries.map((oneCountry, count) => {
Expand Down
9 changes: 3 additions & 6 deletions utils/getCountry.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
const chalk = require('chalk');
const axios = require('axios');
const sym = require('log-symbols');
const comma = require('comma-number');
const red = chalk.red;
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const fetchAndHandleErrors = require('./fetchAndHandleErrors.js');

module.exports = async (spinner, table, states, countryName, options) => {
if (countryName && !states && !options.chart) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/countries/${countryName}`)
const { response } = await fetchAndHandleErrors(
`https://corona.lmao.ninja/countries/${countryName}`
);
handleError(`API is down, try again later.`, err, false);
const thisCountry = response.data;

if (response.data === 'Country not found') {
Expand Down
9 changes: 3 additions & 6 deletions utils/getCountryChart.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
const comma = require('comma-number');
const handleError = require('cli-handle-error');
const axios = require('axios');
const to = require('await-to-js').default;
const moment = require('moment');
const blessed = require('blessed');
const contrib = require('blessed-contrib');
const fetchAndHandleErrors = require('./fetchAndHandleErrors.js');

module.exports = async (spinner, countryName, { chart, log }) => {
if (countryName && chart) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/v2/historical/${countryName}`)
const { response } = await fetchAndHandleErrors(
`https://corona.lmao.ninja/v2/historical/${countryName}`
);
handleError(`API is down, try again later.`, err, false);
if (response.status === 404) {
spinner.stopAndPersist();
console.log(
Expand Down
9 changes: 3 additions & 6 deletions utils/getStates.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const axios = require('axios');
const chalk = require('chalk');
const cyan = chalk.cyan;
const dim = chalk.dim;
const comma = require('comma-number');
const { sortingStateKeys } = require('./table.js');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const orderBy = require('lodash.orderby');
const fetchAndHandleErrors = require('./fetchAndHandleErrors.js');

module.exports = async (spinner, table, states, { sortBy, limit, reverse }) => {
if (states) {
const [err, response] = await to(
axios.get(`https://corona.lmao.ninja/states`)
const { response } = await fetchAndHandleErrors(
`https://corona.lmao.ninja/states`
);
handleError(`API is down, try again later.`, err, false);
let allStates = response.data;

// Limit.
Expand Down
10 changes: 5 additions & 5 deletions utils/getWorldwide.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const axios = require('axios');
const comma = require('comma-number');
const to = require('await-to-js').default;
const handleError = require('cli-handle-error');
const fetchAndHandleErrors = require('./fetchAndHandleErrors.js');

module.exports = async (table, states) => {
const [err, all] = await to(axios.get(`https://corona.lmao.ninja/all`));
handleError(`API is down, try again later.`, err, false);
const { response: all } = await fetchAndHandleErrors(
`https://corona.lmao.ninja/all`
);

let data = Object.values(all.data);
data = data.map(d => comma(d));

Expand Down