Skip to content

Commit

Permalink
Merge pull request #357 from developmentseed/fix/logger-helper
Browse files Browse the repository at this point in the history
Use logger helper consistently
  • Loading branch information
vgeorge authored Dec 23, 2022
2 parents 0885362 + 5213fc0 commit a433b73
Show file tree
Hide file tree
Showing 37 changed files with 148 additions and 107 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"next/core-web-vitals",
"plugin:prettier/recommended",
"plugin:cypress/recommended"
]
],
"rules": {
"no-console": 2
}
}
5 changes: 3 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const cors = require('cors')

const manageRouter = require('./manage')
const oauthRouter = require('./oauth')
const logger = require('../src/lib/logger')

const dev = process.env.NODE_ENV !== 'production'
const PORT = process.env.PORT || 8989
Expand Down Expand Up @@ -60,7 +61,7 @@ async function init() {
return nextApp.render(req, res, '/uh-oh')
}
res.status(err.status || 500)
console.error('error', err)
logger.error('error', err)
res.boom.internal('An internal error occurred.')
})

Expand All @@ -71,7 +72,7 @@ async function init() {
if (require.main === module) {
init().then((app) => {
app.listen(PORT, () => {
console.log(`Starting server on port ${PORT}`)
logger.info(`Starting server on port ${PORT}`)
})
})
}
Expand Down
9 changes: 5 additions & 4 deletions app/lib/hydra.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const qs = require('qs')
const { URL } = require('url')

const { serverRuntimeConfig } = require('../../next.config')
const logger = require('../../src/lib/logger')
const hydraUrl = serverRuntimeConfig.HYDRA_ADMIN_HOST

var mockTlsTermination = {}
Expand All @@ -29,7 +30,7 @@ function get(flow, challenge) {
// This will handle any errors that aren't network related (network related errors are handled automatically)
return res.json().then(function (body) {
if (res.status !== 404) {
console.error('An error occurred while making a HTTP request: ', body)
logger.error('An error occurred while making a HTTP request: ', body)
}
return Promise.reject(new Error(body.error.message))
})
Expand Down Expand Up @@ -59,7 +60,7 @@ function put(flow, action, challenge, body) {
// This will handle any errors that aren't network related (network related errors are handled automatically)
return res.json().then(function (body) {
if (res.status !== 404) {
console.error('An error occurred while making a HTTP request: ', body)
logger.error('An error occurred while making a HTTP request: ', body)
}
return Promise.reject(new Error(body.error.message))
})
Expand All @@ -81,7 +82,7 @@ function getClients() {
// This will handle any errors that aren't network related (network related errors are handled automatically)
return res.json().then(function (body) {
if (res.status !== 404) {
console.error('An error occurred while making a HTTP request: ', body)
logger.error('An error occurred while making a HTTP request: ', body)
}
return Promise.reject(new Error(body.error.message))
})
Expand All @@ -104,7 +105,7 @@ function createClient(body) {
// This will handle any errors that aren't network related (network related errors are handled automatically)
return res.json().then(function (body) {
if (res.status !== 404) {
console.error('An error occurred while making a HTTP request: ', body)
logger.error('An error occurred while making a HTTP request: ', body)
}
return Promise.reject(new Error(body.error.message))
})
Expand Down
3 changes: 0 additions & 3 deletions app/lib/logger.js

This file was deleted.

3 changes: 2 additions & 1 deletion app/lib/osm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const InternalOAuthError = require('passport-oauth').InternalOAuthError
const OSMStrategy = require('passport-openstreetmap').Strategy

const { serverRuntimeConfig } = require('../../next.config')
const logger = require('../../src/lib/logger')

// get an authentication token pair from openstreetmap
function openstreetmap(req, res) {
Expand Down Expand Up @@ -118,7 +119,7 @@ function openstreetmap(req, res) {
}
})
.catch((e) => {
console.error(e)
logger.error(e)
return res.redirect('/')
})
} else {
Expand Down
19 changes: 10 additions & 9 deletions app/manage/badges.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const profile = require('../../src/models/profile')
const { routeWrapper } = require('./utils')
const team = require('../../src/models/team')
const Boom = require('@hapi/boom')
const logger = require('../../src/lib/logger')

/**
* Get the list of badges of an organization
Expand All @@ -25,7 +26,7 @@ const listBadges = routeWrapper({
.orderBy('id')
reply.send(badges)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down Expand Up @@ -58,7 +59,7 @@ const createBadge = routeWrapper({
.returning('*')
reply.send(badge)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down Expand Up @@ -119,7 +120,7 @@ const getBadge = routeWrapper({
users,
})
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down Expand Up @@ -151,7 +152,7 @@ const patchBadge = routeWrapper({
.returning('*')
reply.send(badge)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand All @@ -176,7 +177,7 @@ const deleteBadge = routeWrapper({
message: `Badge ${req.params.badgeId} deleted successfully.`,
})
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down Expand Up @@ -224,7 +225,7 @@ const assignUserBadge = routeWrapper({

reply.send(badge)
} catch (err) {
console.log(err)
logger.error(err)
if (err.code === '23505') {
throw Boom.badRequest('User is already assigned to badge.')
} else {
Expand All @@ -250,7 +251,7 @@ const listUserBadges = routeWrapper({
const badges = await profile.getUserBadges(req.params.userId)
reply.send({ badges })
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down Expand Up @@ -291,7 +292,7 @@ const updateUserBadge = routeWrapper({

reply.send(badge)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down Expand Up @@ -322,7 +323,7 @@ const removeUserBadge = routeWrapper({
message: `Badge ${req.params.badgeId} unassigned successfully.`,
})
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
},
Expand Down
7 changes: 4 additions & 3 deletions app/manage/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { serverRuntimeConfig } = require('../../next.config')
const jwt = require('jsonwebtoken')
const db = require('../../src/lib/db')
const logger = require('../../src/lib/logger')

const APP_URL = process.env.APP_URL

Expand Down Expand Up @@ -50,7 +51,7 @@ async function loginAccept(req, res) {
*/
if (state !== req.session.login_csrf) {
req.session.destroy(function (err) {
if (err) console.error(err)
if (err) logger.error(err)
return res.status(500).json('State does not match')
})
} else {
Expand Down Expand Up @@ -78,7 +79,7 @@ async function loginAccept(req, res) {
req.session.idToken = result.id_token
return res.redirect(`${APP_URL}/profile`)
} catch (error) {
console.error(error)
logger.error(error)
return res.status(500).json('Authentication failed')
}
}
Expand All @@ -91,7 +92,7 @@ async function loginAccept(req, res) {
*/
function logout(req, res) {
req.session.destroy(function (err) {
if (err) console.error(err)
if (err) logger.error(err)
res.redirect(APP_URL)
})
}
Expand Down
15 changes: 8 additions & 7 deletions app/manage/organizations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const organization = require('../../src/models/organization')
const Boom = require('@hapi/boom')
const logger = require('../../src/lib/logger')

/**
* List organizations that a user is a member of
Expand All @@ -10,7 +11,7 @@ async function listMyOrgs(req, reply) {
const orgs = await organization.listMyOrganizations(user_id)
reply.send(orgs)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand All @@ -28,7 +29,7 @@ async function createOrg(req, reply) {
const data = await organization.create(body, user_id)
reply.send(data)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand Down Expand Up @@ -76,7 +77,7 @@ async function updateOrg(req, reply) {
const data = await organization.update(id, body)
reply.send(data)
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand All @@ -95,7 +96,7 @@ async function destroyOrg(req, reply) {
await organization.destroy(id)
return reply.status(200).send()
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand All @@ -118,7 +119,7 @@ async function addOwner(req, reply) {
await organization.addOwner(id, Number(osmId))
return reply.status(200).send()
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand All @@ -141,7 +142,7 @@ async function removeOwner(req, reply) {
await organization.removeOwner(id, Number(osmId))
return reply.status(200).send()
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand Down Expand Up @@ -182,7 +183,7 @@ async function removeManager(req, reply) {
await organization.removeManager(id, Number(osmId))
return reply.status(200).send()
} catch (err) {
console.log(err)
logger.error(err)
throw Boom.badRequest(err.message)
}
}
Expand Down
15 changes: 8 additions & 7 deletions app/manage/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const org = require('../../src/models/organization')
const { pick, prop, assoc } = require('ramda')
const { ValidationError, PropertyRequiredError } = require('../lib/utils')
const Boom = require('@hapi/boom')
const logger = require('../../src/lib/logger')

/**
* Gets a user profile in an org
Expand Down Expand Up @@ -297,7 +298,7 @@ function createProfileKeys(ownerType, profileType) {
const data = await profile.addProfileKeys(attributesToAdd, ownerType, id)
return reply.send(data)
} catch (err) {
console.error(err)
logger.error(err)
if (
err instanceof ValidationError ||
err instanceof PropertyRequiredError
Expand All @@ -324,7 +325,7 @@ async function modifyProfileKey(req, reply) {
await profile.modifyProfileKey(id, body)
return reply.status(200).send()
} catch (err) {
console.error(err)
logger.error(err)
if (
err instanceof ValidationError ||
err instanceof PropertyRequiredError
Expand All @@ -349,7 +350,7 @@ async function deleteProfileKey(req, reply) {
await profile.deleteProfileKey(id)
return reply.status(200).send()
} catch (err) {
console.error(err)
logger.error(err)
if (
err instanceof ValidationError ||
err instanceof PropertyRequiredError
Expand Down Expand Up @@ -379,7 +380,7 @@ function getProfileKeys(ownerType, profileType) {
)
return reply.send(data)
} catch (err) {
console.error(err)
logger.error(err)
if (
err instanceof ValidationError ||
err instanceof PropertyRequiredError
Expand Down Expand Up @@ -410,7 +411,7 @@ function setProfile(profileType) {
await profile.setProfile(body, profileType, id)
reply.status(200).send()
} catch (err) {
console.error(err)
logger.error(err)
throw Boom.badImplementation()
}
}
Expand All @@ -425,7 +426,7 @@ async function getMyProfile(req, reply) {
const data = await profile.getProfile('user', user_id)
return reply.send(data)
} catch (err) {
console.error(err)
logger.error(err)
throw Boom.badImplementation()
}
}
Expand All @@ -437,7 +438,7 @@ async function setMyProfile(req, reply) {
await profile.setProfile(body, 'user', user_id)
return reply.status(200).send()
} catch (err) {
console.error(err)
logger.error(err)
throw Boom.badImplementation()
}
}
Expand Down
Loading

0 comments on commit a433b73

Please sign in to comment.