Skip to content

Commit afb399c

Browse files
committed
📦 NEW: API down handler
1 parent 4ab3a9c commit afb399c

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"test": "node test.js"
2828
},
2929
"dependencies": {
30+
"await-to-js": "^2.1.1",
3031
"axios": "^0.19.2",
3132
"chalk": "^3.0.0",
3233
"cli-handle-error": "^3.0.0",

utils/getAll.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ const axios = require("axios");
22
const chalk = require("chalk");
33
const comma = require("comma-number");
44
const { sortKeys, sortOrders } = require("./table.js");
5+
const to = require("await-to-js").default;
6+
const handleError = require("cli-handle-error");
57

68
module.exports = async (spinner, table, states, country, options) => {
79
if (!country && !states) {
8-
const api = await axios.get(`https://corona.lmao.ninja/countries`);
10+
const [err, api] = await to(
11+
axios.get(`https://corona.lmao.ninja/countries`)
12+
);
13+
handleError(`API is down, try again later.`, err, false);
14+
915
let allCountries = api.data.map(country => Object.values(country));
1016

1117
const sortIndex = sortKeys.indexOf(options.sort);

utils/getCountry.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ const axios = require("axios");
33
const logSymbols = require("log-symbols");
44
const comma = require("comma-number");
55
const red = chalk.red;
6-
const green = chalk.green;
6+
const to = require("await-to-js").default;
7+
const handleError = require("cli-handle-error");
78

89
module.exports = async (spinner, table, states, country) => {
910
if (country && !states) {
10-
const api = await axios.get(
11-
`https://corona.lmao.ninja/countries/${country}`
11+
const [err, api] = await to(
12+
axios.get(`https://corona.lmao.ninja/countries/${country}`)
1213
);
14+
handleError(`API is down, try again later.`, err, false);
15+
1316
if (api.data === "Country not found") {
1417
spinner.stopAndPersist();
1518
console.log(

utils/getStates.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ const axios = require("axios");
22
const chalk = require("chalk");
33
const comma = require("comma-number");
44
const { sortStateKeys, sortStateOrders } = require("./table.js");
5+
const to = require("await-to-js").default;
6+
const handleError = require("cli-handle-error");
57

68
module.exports = async (spinner, table, states, options) => {
79
if (states) {
8-
const api = await axios.get(`https://corona.lmao.ninja/states`);
9-
let allStates = api.data.map(one => Object.values(one));
10+
const [err, api] = await to(axios.get(`https://corona.lmao.ninja/states`));
11+
handleError(`API is down, try again later.`, err, false);
1012

13+
let allStates = api.data.map(one => Object.values(one));
1114
const sortIndex = sortStateKeys.indexOf(options.sort);
1215

1316
if (sortIndex != -1) {

utils/getWorldwide.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
const axios = require("axios");
22
const comma = require("comma-number");
3+
const to = require("await-to-js").default;
4+
const handleError = require("cli-handle-error");
35

46
module.exports = async (table, states) => {
57
if (!states) {
6-
const all = await axios.get(`https://corona.lmao.ninja/all`);
8+
const [err, all] = await to(axios.get(`https://corona.lmao.ninja/all`));
9+
handleError(`API is down, try again later.`, err, false);
10+
711
let data = Object.values(all.data);
812
data = data.map(d => comma(d));
913
table.push([

0 commit comments

Comments
 (0)