@@ -7,18 +7,23 @@ const port = process.env.PORT || 3001;
77const { getCountryTable, getJSONData, getJSONDataForCountry } = require ( './lib/byCountry' ) ;
88const { 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+
1015app . 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
2429app . 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
0 commit comments