From 88cb09387c978f01d62c5583deb65b727f6ea3ef Mon Sep 17 00:00:00 2001 From: Shubham Mahajan Date: Tue, 11 Jan 2022 17:08:01 +0400 Subject: [PATCH] Improvement in logging and error handling (#227) * [x][improvements] Add Logging to the frontend Application * [x][improvements] Add the try, catch and log for the ckan response * [x][improvements] Update Logging from console to logger --- index.js | 14 +++++++++++++- lib/dms.js | 10 +++++++++- package.json | 1 + yarn.lock | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2ea6491d..df49c8ad 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const moment = require('moment') const redis = require('redis') const fetch = require('node-fetch') const { URL, resolve } = require('url') - +const morgan = require('morgan'); const config = require('./config') const dmsRoutes = require('./routes/dms') @@ -155,6 +155,18 @@ module.exports.makeApp = function () { authRoutes(app) } +// Get real IP address from server + morgan.token("remote-addr", function (req) { + return ( + req.headers["x-real-ip"] || + req.headers["x-forwarded-for"] || + req.ip || + req._remoteAddress || + (req.connection && req.connection.remoteAddress) || undefined); + }); + + app.use(morgan('combined')) + app.use((req, res, next) => { res.locals.message = req.flash('info') next() diff --git a/lib/dms.js b/lib/dms.js index dd6685e2..a71f9926 100644 --- a/lib/dms.js +++ b/lib/dms.js @@ -24,7 +24,15 @@ class DmsModel { if (response.status !== 200) { throw response } - response = await response.json() + + try { + response = await response.json() + } + catch(err) { + logger.info("Response", response) + logger.error("Error", err) + response = await response.json() + } return response } diff --git a/package.json b/package.json index 8eadd89b..5affa340 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "markdown-it": "^8.4.2", "md5": "^2.2.1", "moment": "^2.24.0", + "morgan": "^1.10.0", "nconf": "^0.10.0", "node-fetch": "^2.6.1", "nodemailer": "^6.4.16", diff --git a/yarn.lock b/yarn.lock index e0e23fa7..e8286f4d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -799,6 +799,13 @@ base@^0.11.1: mixin-deep "^1.2.0" pascalcase "^0.1.1" +basic-auth@~2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a" + integrity sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg== + dependencies: + safe-buffer "5.1.2" + batch@0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/batch/-/batch-0.6.1.tgz#dc34314f4e679318093fc760272525f94bf25c16" @@ -3951,6 +3958,17 @@ moment@^2.24.0: version "2.24.0" resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b" +morgan@^1.10.0: + version "1.10.0" + resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7" + integrity sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ== + dependencies: + basic-auth "~2.0.1" + debug "2.6.9" + depd "~2.0.0" + on-finished "~2.3.0" + on-headers "~1.0.2" + ms@2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"