Alv-no/alv-website

Mail API needs some love

Opened this issue · 0 comments

There are some issues with the Mail API that needs to be addressed:

  • Error handling when the user sends in a bad request to the /send endpoint
  • Logging (there needs to be a correlation-ID that goes through the request to simplify debuggin)
  • Send method are missing proper handling of the response. Error below shows there being missing return statements and therefore the logs are blowing up
  • There are no unit-tests covering the logic nor the endpoints
Email sent to hei@alv.no
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (internal/errors.js:322:7)
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
    at ServerResponse.contentType (/app/node_modules/express/lib/response.js:599:15)
    at ServerResponse.sendStatus (/app/node_modules/express/lib/response.js:357:8)
    at /app/packages/mail-api/src/index.js:59:13
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  code: 'ERR_HTTP_HEADERS_SENT'
}
(node:36) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (internal/errors.js:322:7)
    at ServerResponse.setHeader (_http_outgoing.js:561:11)
    at ServerResponse.header (/app/node_modules/express/lib/response.js:771:10)
    at ServerResponse.contentType (/app/node_modules/express/lib/response.js:599:15)
    at ServerResponse.sendStatus (/app/node_modules/express/lib/response.js:357:8)
    at /app/packages/mail-api/src/index.js:63:13
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:36) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 21)
const log = (correlationId, message, status) => {
    timestamp = new Date().toString();
    console.log(`${timestamp} - ${correlationId} - ${message}`)
}
const randomNumberString = () => {
  return `${Math.random()*10000000000000000}`;
}

const genererateGuid = () => {
  return `${randomNumberString()}-${randomNumberString()}`;
}