Skip to content

Commit 196c9e9

Browse files
committed
added errorhandler, all country json result is flattened
1 parent ace2150 commit 196c9e9

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

app.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ const port = process.env.PORT || 3001;
77
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
88
const { getCompleteTable } = require('./lib/corona');
99

10+
function errorHandler(error, res) {
11+
console.error(error);
12+
return res.send('I am sorry. Something went wrong. Please report it');
13+
}
14+
1015
app.get('/', (req, res) => {
1116
const format = req.query.format ? req.query.format : '';
1217

1318
if (format.toLowerCase() === 'json') {
1419
return getJSONData().then(result => {
1520
return res.json(result);
16-
}).catch(error => res.send(error));
21+
}).catch(error => errorHandler(error, res));
1722
}
1823

1924
return getCompleteTable().then(result => {
2025
return res.send(result);
21-
}).catch(error => res.send(error));
26+
}).catch(error => errorHandler(error, res));
2227
});
2328

2429
app.get('/:country', (req, res) => {
@@ -30,12 +35,12 @@ app.get('/:country', (req, res) => {
3035
if (format.toLowerCase() === 'json') {
3136
return getJSONData().then(result => {
3237
return res.json(result);
33-
}).catch(error => res.send(error));
38+
}).catch(error => errorHandler(error, res));
3439
}
3540

3641
return getCompleteTable().then(result => {
3742
return res.send(result);
38-
}).catch(error => res.send(error));
43+
}).catch(error => errorHandler(error, res));
3944
}
4045

4146
try {
@@ -60,12 +65,12 @@ app.get('/:country', (req, res) => {
6065
if (format.toLowerCase() === 'json') {
6166
return getJSONDataForCountry(iso2).then(result => {
6267
return res.json(result);
63-
}).catch(error => res.send(error));
68+
}).catch(error => errorHandler(error, res));
6469
}
6570

6671
return getCountryTable(iso2).then(result => {
6772
return res.send(result);
68-
}).catch(error => res.send(error));
73+
}).catch(error => errorHandler(error, res));
6974
});
7075

7176

lib/byCountry.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ function extraStats(dataArr) {
6464

6565
exports.getJSONData = async () => {
6666
const data = await api.getCoronaData();
67-
return data;
67+
const { latest, confirmed, deaths, recovered } = data;
68+
const countryData = getDataByState(confirmed, deaths, recovered);
69+
return countryData;
6870
}
6971

7072
exports.getJSONDataForCountry = async (countryCode) => {

0 commit comments

Comments
 (0)