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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .github/charts.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,88 @@ I have released a video course to help you become a better developer — <a href

<br>

### RELEASE: [4.1.2](https://github.com/ahmadawais/corona-cli/compare/4.1.1...4.1.2)

![🐛 FIX:](https://img.shields.io/badge/-FIX-gray.svg?colorB=ff6347)

> 🐛 Lingo [`d468c4a`](https://github.com/ahmadawais/corona-cli/commit/d468c4a9f5fe24e60f6d4d6ea549086085e94b67) <br>

<br>

[![hr](https://raw.githubusercontent.com/ahmadawais/stuff/master/images/git/hr.png)](/)

<br>

### RELEASE: [4.1.1](https://github.com/ahmadawais/corona-cli/compare/4.1.0...4.1.1)

![👌 IMPROVE:](https://img.shields.io/badge/-IMPROVEMENT-gray.svg?colorB=39AA54)

> 👌 Help [`dbe69e5`](https://github.com/ahmadawais/corona-cli/commit/dbe69e5a8025b307bd93b12f10588e3a8797effd) <br>

![📖 DOC:](https://img.shields.io/badge/-DOCS-gray.svg?colorB=978CD4)

> 📖 Changelog [`abc1e61`](https://github.com/ahmadawais/corona-cli/commit/abc1e61398b5d812b2a1efb28ebb8949bb6cd6fb) <br>

<br>

[![hr](https://raw.githubusercontent.com/ahmadawais/stuff/master/images/git/hr.png)](/)

<br>

### RELEASE: [4.1.0](https://github.com/ahmadawais/corona-cli/compare/4.0.0...4.1.0)

![🐛 FIX:](https://img.shields.io/badge/-FIX-gray.svg?colorB=ff6347)

> 🐛 Worldwide cases [`09e21ba`](https://github.com/ahmadawais/corona-cli/commit/09e21ba64bf37248be7bdb899fbf9fc4111cbada) <br>

![📖 DOC:](https://img.shields.io/badge/-DOCS-gray.svg?colorB=978CD4)

> 📖 Changelog [`6dd4396`](https://github.com/ahmadawais/corona-cli/commit/6dd439625d8a5f150d893e15f513a88ff8ae1062) <br>

<br>

[![hr](https://raw.githubusercontent.com/ahmadawais/stuff/master/images/git/hr.png)](/)

<br>

### RELEASE: [4.0.0](https://github.com/ahmadawais/corona-cli/compare/3.8.6...4.0.0)

![📦 NEW:](https://img.shields.io/badge/-NEW-gray.svg?colorB=3778FF)

> 📦 Chart for country queries (#66) thanks @ozanerturk [`9dff8fe`](https://github.com/ahmadawais/corona-cli/commit/9dff8fe28aa303bbdacccd510a23b6f44e688d33) <br>
> 📦 Charts [`6d5b21a`](https://github.com/ahmadawais/corona-cli/commit/6d5b21aba98cb35a143ebd33a1152a9bd232e606) <br>
> 📦 FOSS Funding [`7a5c07d`](https://github.com/ahmadawais/corona-cli/commit/7a5c07d29e48659335467f7270ea2e48ad77cc4f) <br>

![👌 IMPROVE:](https://img.shields.io/badge/-IMPROVEMENT-gray.svg?colorB=39AA54)

> 👌 Node check [`079da16`](https://github.com/ahmadawais/corona-cli/commit/079da16f8acb61a43e0ca64556e9c2a0bee9c876) <br>
> 👌 Charts [`964b1c4`](https://github.com/ahmadawais/corona-cli/commit/964b1c42363d2807b5bee908dd88d2db9b9c2c66) <br>
> 👌 Chart [`86b889a`](https://github.com/ahmadawais/corona-cli/commit/86b889a9210216f7074efe26b2c498b40d526461) <br>

![🐛 FIX:](https://img.shields.io/badge/-FIX-gray.svg?colorB=ff6347)

> 🐛 Sorting China [`e3c67e5`](https://github.com/ahmadawais/corona-cli/commit/e3c67e529f1ede28b0263e642af67a60a213182e) <br>

![📖 DOC:](https://img.shields.io/badge/-DOCS-gray.svg?colorB=978CD4)

> 📖 Changelog [`bd32c4e`](https://github.com/ahmadawais/corona-cli/commit/bd32c4e81fcf249f4ee222c2db869482b53b779e) <br>

<br>

[![hr](https://raw.githubusercontent.com/ahmadawais/stuff/master/images/git/hr.png)](/)

<br>

### RELEASE: [3.8.6](https://github.com/ahmadawais/corona-cli/compare/3.8.5...3.8.6)

![📦 NEW:](https://img.shields.io/badge/-NEW-gray.svg?colorB=3778FF)

> 📦 Keywords [`02916cd`](https://github.com/ahmadawais/corona-cli/commit/02916cd10d2e93da0ce5e24c2e80a000b5251e6b) <br>

![📖 DOC:](https://img.shields.io/badge/-DOCS-gray.svg?colorB=978CD4)

> 📖 Changelog [`148004a`](https://github.com/ahmadawais/corona-cli/commit/148004a2dbca4b05579ea9d7eed6fe9cd277fe71) <br>

<br>

[![hr](https://raw.githubusercontent.com/ahmadawais/stuff/master/images/git/hr.png)](/)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "corona-cli",
"version": "3.8.6",
"version": "4.1.2",
"description": "Track the Coronavirus disease (COVID-19).",
"license": "MIT",
"repository": "ahmadawais/corona-cli",
Expand Down 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
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Track the Coronavirus disease (COVID-19) or the Novel Coronavirus Strain.
- 🤯 Active daily reporting of your country's COVID-19 statistics
- 🇺🇸 Get US States data for Coronavirus disease reports across the US
- 🗃️ Data: Country, Cases, Deaths, Recovered, Active, Critical, Per Million
- 📈 Charts: Plot statistics in the form of line charts both regular and logarithmic
- 📟 Sort: `cases`, `cases-today`, `deaths`, `deaths-today`, `recovered`, `active`, `critical`, `per-million`

<br>
Expand Down Expand Up @@ -113,6 +114,20 @@ corona -s active -r

[![📟](./.github/sort.gif)](./../../)

### Charts: Regular & Logarithmic

```sh
# Print a country line chart.
corona usa --chart
corona usa --c

# Print a country line chart with logarithmic data.
corona china --chart --log
corona china -c -g
```

[![📟](./.github/charts.gif)](./../../)

### Limit the output

````sh
Expand Down
21 changes: 13 additions & 8 deletions utils/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ module.exports = meow(
${green(`corona`)} ${cyan(`<command>`)} ${yellow(`[--option]`)}

Commands
${cyan(`country-name`)} Get data for a given country
${cyan(`states`)} Get data for all USA states
${cyan(`country-name`)} Get data for a given country
${cyan(`states`)} Get data for all USA states

Options
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type
${yellow(`--reverse`)}, ${yellow(`-r`)} Reverse print order
${yellow(`--limit`)}, ${yellow(`-l`)} Print only N entries
${yellow(`--sort`)}, ${yellow(`-s`)} Sort data by type
${yellow(`--reverse`)}, ${yellow(`-r`)} Reverse print order
${yellow(`--limit`)}, ${yellow(`-l`)} Print only N entries
${yellow(`--chart`)}, ${yellow(`-c`)} Print chart for a country
${yellow(`--log`)}, ${yellow(`-g`)} Print logarithmic chart
${yellow(`--xcolor`)}, ${yellow(`-x`)} Single colored output
${yellow(`--minimal`)}, ${yellow(`-m`)} Minimalistic CLI output

Examples
${green(`corona`)} ${cyan(`china`)}
${green(`corona`)} ${cyan(`states`)}
${green(`corona`)} ${yellow(`-x`)}
${green(`corona`)} ${cyan(`china`)} ${yellow(`--chart`)}
${green(`corona`)} ${cyan(`china`)} ${yellow(`--chart`)} ${yellow(`--log`)}
${green(`corona`)} ${yellow(`--sort`)} ${cyan(`cases-today`)}
${green(`corona`)} ${yellow(`-s`)} ${cyan(`critical`)}

Expand Down Expand Up @@ -61,7 +65,8 @@ module.exports = meow(
},
log: {
type: 'boolean',
default: false
default: false,
alias: 'g'
},
minimal: {
type: 'boolean',
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 };
};
19 changes: 5 additions & 14 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,15 @@ 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);
let allCountries = response.data;

// 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
11 changes: 4 additions & 7 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 All @@ -21,7 +18,7 @@ module.exports = async (spinner, countryName, { chart, log }) => {
);
process.exit(0);
}
const logScale = x => x;
let logScale = x => x;
if (log) {
logScale = x => (x === 0 ? undefined : Math.log(x));
}
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
31 changes: 16 additions & 15 deletions utils/getWorldwide.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
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));

// Don't print coz for states we still need that data of updated data.
if (!states) {
table.push([
``,
``,
`Worldwide`,
data[0],
`—`,
data[1],
`—`,
data[2],
`—`,
`—`,
`—`
comma(allData.cases),
comma(allData.todayCases),
comma(allData.deaths),
comma(allData.todayDeaths),
comma(allData.recovered),
comma(allData.active),
comma(allData.critical),
comma(allData.casesPerOneMillion)
]);
}

const lastUpdated = Date(data[3]);
const lastUpdated = Date(allData.updated);
return lastUpdated;
};