Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed Nov 7, 2023
1 parent 0f790ea commit c2dba7b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"start": "node -r dotenv/config server.js",
"dev": "nodemon -r dotenv/config server.js",
"dev-noenv": "nodemon server.js",
"lint": "eslint . --ext .js",
"lint-fix": "eslint --fix . --ext .js",
"test": "NODE_OPTIONS=--experimental-vm-modules npx mocha --timeout 10000 -r dotenv/config dotenv_config_path=src/test-fixtures/.env.testing src/app.test.js ",
"prepare": "husky install"
},
Expand Down
3 changes: 2 additions & 1 deletion src/SigningException.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default function SigningException(code, message) {
export default function SigningException(code, message, stack) {
this.code = code
this.message = message
this.stack = stack
}
24 changes: 14 additions & 10 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import accessLogger from './middleware/accessLogger.js'
import errorHandler from './middleware/errorHandler.js'
import errorLogger from './middleware/errorLogger.js'
import invalidPathHandler from './middleware/invalidPathHandler.js'
import SigningException from './SigningException.js'

export async function build() {
var app = express()
Expand All @@ -25,23 +26,26 @@ export async function build() {
const instanceId = req.params.instanceId //the issuer instance/tenant with which to sign
const unSignedVC = req.body
if (!req.body || !Object.keys(req.body).length) {
next({
message: 'A verifiable credential must be provided in the body',
code: 400
})
throw new SigningException(
400,
'A verifiable credential must be provided in the body.'
)
}
const signedVC = await issue(unSignedVC, instanceId)
return res.json(signedVC)
} catch (e) {
// we have to catch the async errors and pass them to the error handler
const code = e.code || 500
next({ code, error: e.stack })
// catch the async errors and pass them to the error logger and handler
next(e)
}
})

app.get('/seedgen', async (req, res) => {
const newSeed = await generateSeed()
res.json(newSeed)
app.get('/seedgen', async (req, res, next) => {
try {
const newSeed = await generateSeed()
res.json(newSeed)
} catch (e) {
next(e)
}
})

// Attach the error handling middleware calls, in the order that they should run
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const errorHandler = (error, request, response, next) => {
// and more easily understandable, referring to the logs
// for more detail

const code = error.code | 500
const code = error.code || 500
const message = `An error occurred in the signing-service: ${
error.message || 'unknown error.'
} See the logs for full details. If you are using docker compose, view the logs with 'docker compose logs', and just the signing service logs with: 'docker compose logs signing-serive'`
} See the logs for full details. If you are using docker compose, view the logs with 'docker compose logs', and just the signing service logs with: 'docker compose logs signing-service'`
const errorResponse = { code, message }
response.header('Content-Type', 'application/json')
return response.status(error.code).json(errorResponse)
return response.status(code).json(errorResponse)
}

export default errorHandler
11 changes: 8 additions & 3 deletions src/middleware/errorLogger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import logger from '../utils/logger.js'

const errorLogger = (error, request, response, next) => {
const logEntry = { originalError: error }
logEntry.message = 'An error occurred in the signing-service.'
const logEntrySummary = `Error for route: ${request.originalUrl} - ${request.method} - IP: ${request.ip} - `
const stackTrace = error.stack || 'no stack trace available'
const logEntry = { stackTrace }

const message = `An error occurred in the signing-service: ${
error.message || 'unknown error'
}`
const logEntrySummary = `Error for route: ${request.originalUrl} - ${request.method} - IP: ${request.ip} - ${message}`

// Note that the logEntry here is what Winston calls a 'meta' object.
// Winston simply prints the logEntry to the log as provided - JSON in this case.
// the logEntrySummary is, on the hand, formatted
logger.error(logEntrySummary, logEntry)

next(error) // done logging, so call the next middleware that deals with errors
Expand Down

0 comments on commit c2dba7b

Please sign in to comment.