Added some error logging to the server

This commit is contained in:
Martin Donnelly 2020-01-20 23:15:01 +00:00
parent fcae12997d
commit 960427c906

View File

@ -19,6 +19,9 @@ const sitePath = 'live';
apicache.options({ 'debug': true }); apicache.options({ 'debug': true });
const cache = apicache.middleware; const cache = apicache.middleware;
const standardError = {
'error':'There was an error'
};
app.use(express.static(path.join(__dirname, sitePath))); app.use(express.static(path.join(__dirname, sitePath)));
@ -39,7 +42,7 @@ app.get('/weather', (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'weather', 'e':e }));
}); });
else { else {
@ -60,7 +63,7 @@ app.get('/forecast', (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'forecast', 'e':e }));
}); });
else { else {
@ -80,7 +83,7 @@ app.get('/weatheralert', cache('15 minutes'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'weatheralert', 'e':e }));
}); });
else { else {
@ -96,14 +99,14 @@ app.get('/fsexplore', cache('15 minutes'), (req, res) => {
const limit = req.query.hasOwnProperty('ll') ? req.query.limit : 3; const limit = req.query.hasOwnProperty('ll') ? req.query.limit : 3;
const section = req.query.hasOwnProperty('section') ? req.query.section : 'topPicks'; const section = req.query.hasOwnProperty('section') ? req.query.section : 'topPicks';
const query = req.query.hasOwnProperty('query') ? req.query.query : ''; const query = req.query.hasOwnProperty('query') ? req.query.query : '';
console.log('req.query',req.query); console.log('req.query', req.query);
foursquare.doGetFourSquareExplore(ll, limit, section, query) foursquare.doGetFourSquareExplore(ll, limit, section, query)
.then((d) => { .then((d) => {
res.set('Cache-Control', 'public, max-age=900'); res.set('Cache-Control', 'public, max-age=900');
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'fsexplore', 'e':e }));
}); });
} }
@ -114,17 +117,17 @@ app.get('/fsexplore', cache('15 minutes'), (req, res) => {
} }
}); });
app.get('/geocode', /*cache('15 minutes'),*/ (req, res) => { app.get('/geocode', /* cache('15 minutes'),*/ (req, res) => {
if (req.query.hasOwnProperty('ll')) { if (req.query.hasOwnProperty('ll')) {
const ll = req.query.ll; const ll = req.query.ll;
console.log('ll',ll); console.log('ll', ll);
geocode.doGetGeocode(ll) geocode.doGetGeocode(ll)
.then((d) => { .then((d) => {
res.set('Cache-Control', 'public, max-age=900'); res.set('Cache-Control', 'public, max-age=900');
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'geocode', 'e':e }));
}); });
} }
@ -135,7 +138,6 @@ app.get('/geocode', /*cache('15 minutes'),*/ (req, res) => {
} }
}); });
app.get('/rightbyme', cache('86400 seconds'), (req, res) => { app.get('/rightbyme', cache('86400 seconds'), (req, res) => {
if (req.query.hasOwnProperty('ll')) if (req.query.hasOwnProperty('ll'))
rightbyme.doGetRightByMe(req.query.ll) rightbyme.doGetRightByMe(req.query.ll)
@ -144,7 +146,7 @@ app.get('/rightbyme', cache('86400 seconds'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'rightbyme', 'e':e }));
}); });
else { else {
@ -162,7 +164,7 @@ app.get('/nearbydetail', cache('15 minutes'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'nearbydetail', 'e':e }));
}); });
else { else {
@ -183,7 +185,7 @@ app.get('/news', cache('15 minutes'), (req, res) => {
} }
else { else {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'news', 'e':e }));
} }
}); });
}); });
@ -198,7 +200,7 @@ app.get('/article', cache('1 hour'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'article', 'e':e }));
}); });
} }
else { else {
@ -215,7 +217,7 @@ app.get('/agenda', cache('15 minutes'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'agenda', 'e':e }));
}); });
}); });
@ -229,7 +231,7 @@ app.get('/oldtraffic', cache('5 minutes'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'oldtraffic', 'e':e }));
}); });
else { else {
@ -248,7 +250,7 @@ app.get('/incidents', cache('5 minutes'), (req, res) => {
res.send(d); res.send(d);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('directions.getIncidents: There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'incidents', 'e':e }));
}); });
}); });
@ -258,12 +260,12 @@ app.get('/traffic', cache('5 minutes'), asyncMiddleware(async (req, res, next) =
if (req.query.hasOwnProperty('olat')) if (req.query.hasOwnProperty('olat'))
await directions.doGetEstDirectionsWithIncidents(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon).then((b) => { await directions.doGetEstDirectionsWithIncidents(req.query.olat, req.query.olon, req.query.dlat, req.query.dlon).then((b) => {
//logger.debug('b', b); // logger.debug('b', b);
res.set('Cache-Control', 'public, max-age=300'); res.set('Cache-Control', 'public, max-age=300');
res.send(b); res.send(b);
}).catch((e) => { }).catch((e) => {
logger.error(e); logger.error(e);
res.status(500).send('There was an error!'); res.status(500).send(Object.assign(standardError, { 'source':'traffic', 'e':e }));
}); });
else { else {