diff --git a/package.json b/package.json index f30f795..aa66241 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/SigningException.js b/src/SigningException.js index bf40a77..34fae4b 100644 --- a/src/SigningException.js +++ b/src/SigningException.js @@ -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 } diff --git a/src/app.js b/src/app.js index fc06bb2..b693788 100644 --- a/src/app.js +++ b/src/app.js @@ -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() @@ -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 diff --git a/src/middleware/errorHandler.js b/src/middleware/errorHandler.js index 05ca8d1..ee9d402 100644 --- a/src/middleware/errorHandler.js +++ b/src/middleware/errorHandler.js @@ -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 diff --git a/src/middleware/errorLogger.js b/src/middleware/errorLogger.js index 1f009ae..8f12813 100644 --- a/src/middleware/errorLogger.js +++ b/src/middleware/errorLogger.js @@ -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