Skip to content

Commit eb42bab

Browse files
committed
👌 IMPROVE: Missing Country
1 parent 8a587a0 commit eb42bab

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

utils/exitCountry.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { red } = require('chalk');
2+
const sym = require('log-symbols');
3+
4+
module.exports = (err, spinner, countryName) => {
5+
if (
6+
err &&
7+
err.response &&
8+
err.response.status &&
9+
err.response.status === 404
10+
) {
11+
spinner.stopAndPersist();
12+
console.log(
13+
`${red(
14+
`${sym.error} Oops. A country named "${countryName}" doesn't exist…`
15+
)}\n`
16+
);
17+
process.exit(0);
18+
}
19+
};

utils/getCountry.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
const { red } = require('chalk');
21
const axios = require('axios');
3-
const sym = require('log-symbols');
42
const numberFormat = require('./numberFormat');
3+
const exitCountry = require('./exitCountry');
54
const to = require('await-to-js').default;
65
const handleError = require('cli-handle-error');
76

@@ -10,17 +9,8 @@ module.exports = async (spinner, table, states, countryName, options) => {
109
const [err, response] = await to(
1110
axios.get(`https://corona.lmao.ninja/v2/countries/${countryName}`)
1211
);
13-
14-
if (err && err.response.status === 404) {
15-
spinner.stopAndPersist();
16-
console.log(
17-
`${red(
18-
`${sym.error} Oops. A country named "${countryName}" does not exist or has no cases...`
19-
)}\n`
20-
);
21-
process.exit(0);
22-
}
23-
12+
exitCountry(err, spinner, countryName);
13+
err && spinner.stopAndPersist();
2414
handleError(`API is down, try again later.`, err, false);
2515
const thisCountry = response.data;
2616

0 commit comments

Comments
 (0)