Skip to content

Commit

Permalink
Improvement in logging and error handling (#227)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
shubham-mahajan authored Jan 11, 2022
1 parent aef49a3 commit 88cb093
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 9 additions & 1 deletion lib/dms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

[email protected]:
version "0.6.1"
resolved "https://registry.yarnpkg.com/batch/-/batch-0.6.1.tgz#dc34314f4e679318093fc760272525f94bf25c16"
Expand Down Expand Up @@ -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"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit 88cb093

Please sign in to comment.