diff --git a/sanitas_backend/layers/utils/utils/index.mjs b/sanitas_backend/layers/utils/utils/index.mjs index aeac2ab2..e5766677 100644 --- a/sanitas_backend/layers/utils/utils/index.mjs +++ b/sanitas_backend/layers/utils/utils/index.mjs @@ -1100,3 +1100,38 @@ export function mapToAPIMedicalConsultation(dbData) { }, }; } + +/** + * @typedef {Object} SafeEvent + * @property {string} httpMethod - The HTTP method of the event. + * @property {string} path - The path of the event. + * @property {Object} headers - The headers of the event with the Authorization header redacted. + * @property {string} body - The body of the event as a JSON string or "No body" if not present. + */ + +/** + * Converts an event to a safe event by redacting sensitive information. + * + * @param {Object} event - The original event object. + * @param {string} event.httpMethod - The HTTP method of the event. + * @param {string} event.path - The path of the event. + * @param {Object} event.headers - The headers of the event. + * @param {string|Object} [event.body] - The body of the event. + * @returns {SafeEvent} The safe event object with sensitive information redacted. + */ + +/** + * @param {import('aws-lambda').APIGatewayProxyEvent} event + * @returns {SafeEvent} The event with sensitive data redacted. + */ +export function toSafeEvent(event) { + return { + httpMethod: event.httpMethod, + path: event.path, + headers: { + ...event.headers, + Authorization: "[REDACTED]", + }, + body: event.body ? JSON.stringify(event.body) : "No body", + }; +} diff --git a/sanitas_backend/src/handlers/CheckCui/check-cui.mjs b/sanitas_backend/src/handlers/CheckCui/check-cui.mjs index 7609fb17..b6b4ac87 100644 --- a/sanitas_backend/src/handlers/CheckCui/check-cui.mjs +++ b/sanitas_backend/src/handlers/CheckCui/check-cui.mjs @@ -1,6 +1,6 @@ import { getPgClient, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse } from "utils/index.mjs"; +import { createResponse, toSafeEvent } from "utils/index.mjs"; export const handler = async (event, context) => { withRequest(event, context); @@ -33,7 +33,18 @@ export const handler = async (event, context) => { return responseBuilder.setStatusCode(200).setBody({ exists }).build(); } catch (error) { - logger.error(error, "Error querying database:"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while checking CUI history!", + ); await client?.end(); return responseBuilder diff --git a/sanitas_backend/src/handlers/CreatePatient/create-patient.mjs b/sanitas_backend/src/handlers/CreatePatient/create-patient.mjs index 18107cfc..29bbc007 100644 --- a/sanitas_backend/src/handlers/CreatePatient/create-patient.mjs +++ b/sanitas_backend/src/handlers/CreatePatient/create-patient.mjs @@ -67,7 +67,7 @@ export const createPatientHandler = async (event, context) => { ]); if (existingPatientResult.rows.length > 0) { - logger.error("CUI already exists."); + logger.error(patientData.cui, "CUI already exists."); const response = responseBuilder .setStatusCode(409) diff --git a/sanitas_backend/src/handlers/ExportData/export-data.mjs b/sanitas_backend/src/handlers/ExportData/export-data.mjs index da27ec5c..27cca2e8 100644 --- a/sanitas_backend/src/handlers/ExportData/export-data.mjs +++ b/sanitas_backend/src/handlers/ExportData/export-data.mjs @@ -1,6 +1,6 @@ import { getPgClient, isDoctor, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT } from "utils/index.mjs"; +import { createResponse, decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * Convert a 2D array into a CSV string @@ -56,7 +56,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -115,7 +118,7 @@ export const handler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) @@ -146,6 +149,10 @@ export const handler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -171,8 +178,16 @@ export const handler = async (event, context) => { return responseBuilder.setStatusCode(200).setBody(csv).build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { details: error.message }, + { err: errorDetails, event: safeEvent }, "An error occurred while exporting data!", ); diff --git a/sanitas_backend/src/handlers/GetAllergicHistory/get-allergic-history-patient.mjs b/sanitas_backend/src/handlers/GetAllergicHistory/get-allergic-history-patient.mjs index 9db61beb..dd573c01 100644 --- a/sanitas_backend/src/handlers/GetAllergicHistory/get-allergic-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetAllergicHistory/get-allergic-history-patient.mjs @@ -8,7 +8,11 @@ import { import { logger, withRequest } from "logging"; import { createResponse } from "utils"; import { genDefaultAllergicHistory } from "utils/defaultValues.mjs"; -import { decodeJWT, mapToAPIAllergicHistory } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIAllergicHistory, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP GET request to retrieve allergic medical history for a specific patient by their ID. @@ -37,7 +41,10 @@ export const getAllergicHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -50,7 +57,10 @@ export const getAllergicHistoryHandler = async (event, context) => { try { const patientId = Number.parseInt(event.pathParameters.id, 10); if (!patientId) { - logger.error("Invalid ID received!"); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -69,7 +79,7 @@ export const getAllergicHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) @@ -89,7 +99,10 @@ export const getAllergicHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.error, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) @@ -123,10 +136,16 @@ export const getAllergicHistoryHandler = async (event, context) => { const dbResponse = await client.query(query, args); logger.info("Query done!"); + return dbResponse; }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); + throw transactionResult.error; } @@ -151,8 +170,16 @@ export const getAllergicHistoryHandler = async (event, context) => { const medicalHistory = mapToAPIAllergicHistory(dbResponse.rows[0]); return responseBuilder.setStatusCode(200).setBody(medicalHistory).build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while fetching allergic history!", ); return responseBuilder diff --git a/sanitas_backend/src/handlers/GetConsultationPatient/get-consultation-patient.mjs b/sanitas_backend/src/handlers/GetConsultationPatient/get-consultation-patient.mjs index 7decf3af..02373909 100644 --- a/sanitas_backend/src/handlers/GetConsultationPatient/get-consultation-patient.mjs +++ b/sanitas_backend/src/handlers/GetConsultationPatient/get-consultation-patient.mjs @@ -1,7 +1,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse } from "utils"; -import { decodeJWT, mapToAPIMedicalConsultation } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIMedicalConsultation, + toSafeEvent, +} from "utils/index.mjs"; import { genDefaultMedicalConsultation } from "utils/defaultValues.mjs"; /** @@ -31,7 +35,10 @@ export const getMedicalConsultationHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -42,7 +49,10 @@ export const getMedicalConsultationHandler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -63,7 +73,7 @@ export const getMedicalConsultationHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) @@ -100,6 +110,10 @@ export const getMedicalConsultationHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -126,10 +140,19 @@ export const getMedicalConsultationHandler = async (event, context) => { .setBody({ patientId: patientId, consultations: medicalConsultation }) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( + { err: errorDetails, event: safeEvent }, "An error occurred while fetching medical consultation!", - error, ); + return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/GetFamilyHistory/get-family-history-patient.mjs b/sanitas_backend/src/handlers/GetFamilyHistory/get-family-history-patient.mjs index 0127e4a5..30b7552e 100644 --- a/sanitas_backend/src/handlers/GetFamilyHistory/get-family-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetFamilyHistory/get-family-history-patient.mjs @@ -9,7 +9,7 @@ import { logger, withRequest } from "logging"; import { createResponse } from "utils"; import { mapToAPIFamilyHistory } from "utils"; import { genDefaultFamiliarHistory } from "utils/defaultValues.mjs"; -import { decodeJWT } from "utils/index.mjs"; +import { decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * Handles the HTTP GET request to retrieve family medical history for a specific patient by their ID. @@ -38,7 +38,10 @@ export const getFamilyHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -51,7 +54,10 @@ export const getFamilyHistoryHandler = async (event, context) => { try { const patientId = Number.parseInt(event.pathParameters.id, 10); if (!patientId) { - logger.error("Invalid ID received!"); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -70,7 +76,7 @@ export const getFamilyHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -89,7 +95,10 @@ export const getFamilyHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.error, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -125,6 +134,10 @@ export const getFamilyHistoryHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -149,7 +162,19 @@ export const getFamilyHistoryHandler = async (event, context) => { const medicalHistory = mapToAPIFamilyHistory(dbResponse.rows[0]); return responseBuilder.setStatusCode(200).setBody(medicalHistory).build(); } catch (error) { - logger.error("An error occurred while fetching family history!", error); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching family history!", + ); + return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/GetGeneralCollaboratorInfo/get-collaborator.mjs b/sanitas_backend/src/handlers/GetGeneralCollaboratorInfo/get-collaborator.mjs index 28d1aa9e..3da538be 100644 --- a/sanitas_backend/src/handlers/GetGeneralCollaboratorInfo/get-collaborator.mjs +++ b/sanitas_backend/src/handlers/GetGeneralCollaboratorInfo/get-collaborator.mjs @@ -10,6 +10,7 @@ import { createResponse, decodeJWT, mapToAPICollaboratorInfo, + toSafeEvent, } from "utils/index.mjs"; /** @@ -31,7 +32,10 @@ export const getCollaboratorHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -43,7 +47,7 @@ export const getCollaboratorHandler = async (event, context) => { logger.info("Checking if received all parameters..."); const patientId = event.pathParameters.id; if (!patientId) { - logger.error("No id received!"); + logger.error({ patientId: event.pathParameters?.id }, "No id received!"); const response = responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No id supplied!" }) @@ -66,7 +70,7 @@ export const getCollaboratorHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -85,7 +89,10 @@ export const getCollaboratorHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.error, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -118,6 +125,10 @@ export const getCollaboratorHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -145,7 +156,18 @@ export const getCollaboratorHandler = async (event, context) => { logger.info({ response }, "Responding with:"); return response; } catch (error) { - logger.error(error, "An error has occurred!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching collaborator!", + ); return responseBuilder.setStatusCode(500).setBody(error).build(); } finally { await client?.end(); diff --git a/sanitas_backend/src/handlers/GetGeneralPatientInfo/get-general-patient-info.mjs b/sanitas_backend/src/handlers/GetGeneralPatientInfo/get-general-patient-info.mjs index c4552626..7361b9ab 100644 --- a/sanitas_backend/src/handlers/GetGeneralPatientInfo/get-general-patient-info.mjs +++ b/sanitas_backend/src/handlers/GetGeneralPatientInfo/get-general-patient-info.mjs @@ -6,7 +6,12 @@ import { transaction, } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT, mapToAPIPatient } from "utils/index.mjs"; +import { + createResponse, + decodeJWT, + mapToAPIPatient, + toSafeEvent, +} from "utils/index.mjs"; /** * Get the general patient information endpoint handler. @@ -28,7 +33,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -40,7 +48,7 @@ export const handler = async (event, context) => { logger.info("Checking if received all parameters..."); const patientId = event.pathParameters.id; if (patientId !== 0 && !patientId) { - logger.error("No ID received!"); + logger.error({ patientId: event.pathParameters?.id }, "No ID received!"); const response = responseBuilder .setStatusCode(400) .setBody({ @@ -65,7 +73,7 @@ export const handler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -84,7 +92,10 @@ export const handler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -117,6 +128,10 @@ export const handler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -147,7 +162,18 @@ export const handler = async (event, context) => { logger.info({ response }, "Responding with:"); return response; } catch (error) { - logger.error({ error }, "An error has ocurred!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching general patient info!", + ); const response = responseBuilder.setStatusCode(500).setBody(error).build(); return response; } finally { diff --git a/sanitas_backend/src/handlers/GetGeneralStudentInfo/get-general-student-info.mjs b/sanitas_backend/src/handlers/GetGeneralStudentInfo/get-general-student-info.mjs index a7aae759..2786f6e0 100644 --- a/sanitas_backend/src/handlers/GetGeneralStudentInfo/get-general-student-info.mjs +++ b/sanitas_backend/src/handlers/GetGeneralStudentInfo/get-general-student-info.mjs @@ -10,6 +10,7 @@ import { createResponse, decodeJWT, mapToAPIStudentInfo, + toSafeEvent, } from "utils/index.mjs"; /** @@ -31,7 +32,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -43,7 +47,7 @@ export const handler = async (event, context) => { logger.info("Checking if received all parameters..."); const patientId = Number.parseInt(event.pathParameters.id); if (!patientId) { - logger.error("No id received!"); + logger.error({ patientId: event.pathParameters?.id }, "No id received!"); return responseBuilder .setStatusCode(400) @@ -65,7 +69,7 @@ export const handler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -84,7 +88,10 @@ export const handler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.error, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -118,6 +125,10 @@ export const handler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -154,7 +165,18 @@ export const handler = async (event, context) => { logger.info({ response }, "Responding with:"); return response; } catch (error) { - logger.error(error, "An error has occurred!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching general student info!", + ); return responseBuilder.setStatusCode(500).setBody(error).build(); } finally { await client?.end(); diff --git a/sanitas_backend/src/handlers/GetGynecoObstetricHistory/get-gynecoobstetric-history-patient.mjs b/sanitas_backend/src/handlers/GetGynecoObstetricHistory/get-gynecoobstetric-history-patient.mjs index 6771d691..6713153a 100644 --- a/sanitas_backend/src/handlers/GetGynecoObstetricHistory/get-gynecoobstetric-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetGynecoObstetricHistory/get-gynecoobstetric-history-patient.mjs @@ -10,6 +10,7 @@ import { createResponse, decodeJWT, mapToAPIGynecologicalHistory, + toSafeEvent, } from "utils/index.mjs"; import { genDefaultGynecologicalHistory } from "utils/defaultValues.mjs"; @@ -30,7 +31,10 @@ export const getGynecologicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -41,7 +45,10 @@ export const getGynecologicalHistoryHandler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -62,7 +69,7 @@ export const getGynecologicalHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) @@ -82,7 +89,10 @@ export const getGynecologicalHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -114,6 +124,10 @@ export const getGynecologicalHistoryHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -149,10 +163,19 @@ export const getGynecologicalHistoryHandler = async (event, context) => { logger.info({ response }, "Responding with:"); return response; } catch (error) { - logger.error("An error occurred while fetching gynecological history!", { - error, + const errorDetails = { + message: error.message, stack: error.stack, - }); + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching gynecological history!", + ); + return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/GetLinkedPatient/get-linked-patient.mjs b/sanitas_backend/src/handlers/GetLinkedPatient/get-linked-patient.mjs index ce8e08c1..dbafb609 100644 --- a/sanitas_backend/src/handlers/GetLinkedPatient/get-linked-patient.mjs +++ b/sanitas_backend/src/handlers/GetLinkedPatient/get-linked-patient.mjs @@ -1,7 +1,7 @@ import { getPgClient, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse } from "utils"; -import { decodeJWT } from "utils/index.mjs"; +import { decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * @type {import("src/commonTypes.mjs").AWSHandler} @@ -11,6 +11,8 @@ export const handler = async (event, context) => { const responseBuilder = createResponse().addCORSHeaders("GET"); if (event.httpMethod !== "GET") { + const msg = `/account/patient only accepts GET method, you tried: ${event.httpMethod}`; + logger.error(msg); return responseBuilder .setStatusCode(405) .setBody({ error: "Method Not Allowed" }) @@ -23,7 +25,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -65,8 +70,16 @@ export const handler = async (event, context) => { .setBody({ linkedPatientId }) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while checking if an account has a linked patient", ); diff --git a/sanitas_backend/src/handlers/GetMedicalHistoryMetadata/get-medical-history-metadata.mjs b/sanitas_backend/src/handlers/GetMedicalHistoryMetadata/get-medical-history-metadata.mjs index 52840cf5..d7dc65d4 100644 --- a/sanitas_backend/src/handlers/GetMedicalHistoryMetadata/get-medical-history-metadata.mjs +++ b/sanitas_backend/src/handlers/GetMedicalHistoryMetadata/get-medical-history-metadata.mjs @@ -1,6 +1,6 @@ import { getPgClient, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse } from "utils/index.mjs"; +import { createResponse, toSafeEvent } from "utils/index.mjs"; /** * @typedef {Object} DBMedicalHistoryMetadata @@ -86,7 +86,7 @@ export const handler = async (event, context) => { logger.info("Checking if received all parameters..."); const id = event.pathParameters.id; if (id !== 0 && !id) { - logger.error("No ID received!"); + logger.error({ patientId: event.pathParameters?.id }, "No ID received!"); const response = responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No patientId supplied!" }) @@ -206,8 +206,18 @@ where logger.info(response, "Responding with:"); return response; } catch (error) { - logger.error({ error }, "An error has ocurred!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching medical history metadata!", + ); const response = responseBuilder .setStatusCode(500) .setBody({ error: "An internal server error has ocurred!" }) diff --git a/sanitas_backend/src/handlers/GetNonPatologicalHistory/index.mjs b/sanitas_backend/src/handlers/GetNonPatologicalHistory/index.mjs index 4e43b311..ae293b74 100644 --- a/sanitas_backend/src/handlers/GetNonPatologicalHistory/index.mjs +++ b/sanitas_backend/src/handlers/GetNonPatologicalHistory/index.mjs @@ -11,6 +11,7 @@ import { createResponse, decodeJWT, mapToAPINonPathologicalHistory, + toSafeEvent, } from "utils/index.mjs"; export const handler = async (event, context) => { @@ -30,7 +31,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -41,7 +45,10 @@ export const handler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -62,7 +69,7 @@ export const handler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -81,7 +88,10 @@ export const handler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) @@ -114,6 +124,10 @@ export const handler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -127,7 +141,9 @@ export const handler = async (event, context) => { if (dbResponse.rowCount === 0) { logger.info( "No non-pathological history found! Returning default values...", - { id: patientId }, + { + id: patientId, + }, ); return responseBuilder .setStatusCode(200) @@ -149,10 +165,18 @@ export const handler = async (event, context) => { logger.info(response, "Responding with:"); return response; } catch (error) { - logger.error("An error occurred while fetching non-pathological history!", { - error, + const errorDetails = { + message: error.message, stack: error.stack, - }); + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching non-pathological history!", + ); return responseBuilder .setStatusCode(500) diff --git a/sanitas_backend/src/handlers/GetPersonalHistory/get-personal-history-patient.mjs b/sanitas_backend/src/handlers/GetPersonalHistory/get-personal-history-patient.mjs index 6f710a21..c03270c1 100644 --- a/sanitas_backend/src/handlers/GetPersonalHistory/get-personal-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetPersonalHistory/get-personal-history-patient.mjs @@ -8,7 +8,11 @@ import { import { logger, withRequest } from "logging"; import { createResponse } from "utils"; import { genDefaultPersonalHistory } from "utils/defaultValues.mjs"; -import { decodeJWT, mapToAPIPersonalHistory } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIPersonalHistory, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP GET request to retrieve personal medical history for a specific patient by their ID. @@ -37,7 +41,10 @@ export const getPersonalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -48,7 +55,10 @@ export const getPersonalHistoryHandler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -69,7 +79,7 @@ export const getPersonalHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -88,7 +98,10 @@ export const getPersonalHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.err, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -124,6 +137,10 @@ export const getPersonalHistoryHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -148,7 +165,18 @@ export const getPersonalHistoryHandler = async (event, context) => { const medicalHistory = mapToAPIPersonalHistory(dbResponse.rows[0]); return responseBuilder.setStatusCode(200).setBody(medicalHistory).build(); } catch (error) { - logger.error("An error occurred while fetching personal history!", error); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching personal history!", + ); return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/GetPsychiatricHistory/get-psychiatric-history-patient.mjs b/sanitas_backend/src/handlers/GetPsychiatricHistory/get-psychiatric-history-patient.mjs index 5eb1d9ed..884faa32 100644 --- a/sanitas_backend/src/handlers/GetPsychiatricHistory/get-psychiatric-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetPsychiatricHistory/get-psychiatric-history-patient.mjs @@ -8,7 +8,11 @@ import { import { logger, withRequest } from "logging"; import { createResponse } from "utils"; import { genDefaultPsychiatricHistory } from "utils/defaultValues.mjs"; -import { decodeJWT, mapToAPIPsychiatricHistory } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIPsychiatricHistory, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP GET request to retrieve psychiatric medical history for a specific patient by their ID. @@ -37,7 +41,10 @@ export const getPsychiatricHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -48,7 +55,10 @@ export const getPsychiatricHistoryHandler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -69,7 +79,7 @@ export const getPsychiatricHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -88,7 +98,10 @@ export const getPsychiatricHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.err, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -124,6 +137,10 @@ export const getPsychiatricHistoryHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -148,10 +165,19 @@ export const getPsychiatricHistoryHandler = async (event, context) => { const medicalHistory = mapToAPIPsychiatricHistory(dbResponse.rows[0]); return responseBuilder.setStatusCode(200).setBody(medicalHistory).build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( + { err: errorDetails, event: safeEvent }, "An error occurred while fetching psychiatric history!", - error, ); + return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/GetRole/get-role.mjs b/sanitas_backend/src/handlers/GetRole/get-role.mjs index 5c2f1a60..c31d725d 100644 --- a/sanitas_backend/src/handlers/GetRole/get-role.mjs +++ b/sanitas_backend/src/handlers/GetRole/get-role.mjs @@ -1,6 +1,6 @@ import { getPgClient, isDoctor } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT } from "utils/index.mjs"; +import { createResponse, decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * Handles patient search queries based on ID, employee code, or partial names and surnames. @@ -23,7 +23,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -43,7 +46,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -54,7 +57,18 @@ export const handler = async (event, context) => { logger.info({ response }, "Responding with..."); return response; } catch (error) { - logger.error({ error: error.message }, "An error has occurred!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching role!", + ); return responseBuilder .setStatusCode(500) .setBody({ error: "Internal Server Error" }) diff --git a/sanitas_backend/src/handlers/GetSurgicalHistory/get-surgical-history-patient.mjs b/sanitas_backend/src/handlers/GetSurgicalHistory/get-surgical-history-patient.mjs index 822ccab5..a40b06e3 100644 --- a/sanitas_backend/src/handlers/GetSurgicalHistory/get-surgical-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetSurgicalHistory/get-surgical-history-patient.mjs @@ -11,6 +11,7 @@ import { createResponse, decodeJWT, mapToAPISurgicalHistory, + toSafeEvent, } from "utils/index.mjs"; /** @@ -40,7 +41,10 @@ export const getSurgicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -51,7 +55,10 @@ export const getSurgicalHistoryHandler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -72,7 +79,7 @@ export const getSurgicalHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -91,7 +98,10 @@ export const getSurgicalHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.error, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -123,6 +133,10 @@ export const getSurgicalHistoryHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -153,11 +167,10 @@ export const getSurgicalHistoryHandler = async (event, context) => { .setBody(surgicalHistory) .build(); } catch (error) { - logger.error("An error occurred while processing surgical history:", { - errorMessage: error.message, - errorStack: error.stack, - rawData: dbResponse.rows[0], - }); + logger.error( + error, + "An error occurred while processing surgical history", + ); return responseBuilder .setStatusCode(500) .setBody({ @@ -167,7 +180,18 @@ export const getSurgicalHistoryHandler = async (event, context) => { .build(); } } catch (error) { - logger.error("An error occurred while fetching surgical history!", error); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while fetching surgical history!", + ); return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/GetTraumatologicalHistory/get-traumatological-history-patient.mjs b/sanitas_backend/src/handlers/GetTraumatologicalHistory/get-traumatological-history-patient.mjs index 200be33b..86561d8a 100644 --- a/sanitas_backend/src/handlers/GetTraumatologicalHistory/get-traumatological-history-patient.mjs +++ b/sanitas_backend/src/handlers/GetTraumatologicalHistory/get-traumatological-history-patient.mjs @@ -8,7 +8,7 @@ import { import { logger, withRequest } from "logging"; import { createResponse, mapToAPITraumatologicHistory } from "utils"; import { genDefaultTraumatologicalHistory } from "utils/defaultValues.mjs"; -import { decodeJWT } from "utils/index.mjs"; +import { decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * Handles the HTTP GET request to retrieve traumatologic history for a specific patient by their ID. @@ -37,7 +37,10 @@ export const getTraumatologicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -48,7 +51,10 @@ export const getTraumatologicalHistoryHandler = async (event, context) => { const patientId = Number.parseInt(event.pathParameters?.id, 10); if (Number.isNaN(patientId)) { - logger.error("Invalid ID received!", { id: event.pathParameters?.id }); + logger.error( + { patientId: event.pathParameters?.id }, + "Invalid ID received!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "Invalid request: No valid patientId supplied!" }) @@ -69,7 +75,7 @@ export const getTraumatologicalHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody(itsDoctor) @@ -88,7 +94,10 @@ export const getTraumatologicalHistoryHandler = async (event, context) => { if (emailBelongs.error) { const msg = "An error ocurred while trying to check if the email belongs to the patient!"; - logger.error(emailBelongs, msg); + logger.error( + { err: emailBelongs.error, inputs: { email, patientId } }, + msg, + ); const response = responseBuilder .setStatusCode(500) .setBody(emailBelongs) @@ -120,6 +129,10 @@ export const getTraumatologicalHistoryHandler = async (event, context) => { }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -152,10 +165,19 @@ export const getTraumatologicalHistoryHandler = async (event, context) => { .setBody(traumatologicHistory) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( + { err: errorDetails, event: safeEvent }, "An error occurred while fetching traumatologic history!", - error, ); + return responseBuilder .setStatusCode(500) .setBody({ diff --git a/sanitas_backend/src/handlers/HealthCheck/health-check.mjs b/sanitas_backend/src/handlers/HealthCheck/health-check.mjs index 033d3b9b..6daa2381 100644 --- a/sanitas_backend/src/handlers/HealthCheck/health-check.mjs +++ b/sanitas_backend/src/handlers/HealthCheck/health-check.mjs @@ -35,7 +35,7 @@ export const handler = async (event, context) => { ]) .build(); } catch (error) { - logger.error(error, "Error querying database:"); + logger.error(error, "Error querying database"); return responseBuilder .setStatusCode(200) .setBody([{ name: "DB", status: "DOWN", error: error.message }]) diff --git a/sanitas_backend/src/handlers/LinkAccountToPatient/link-account-to-patient.mjs b/sanitas_backend/src/handlers/LinkAccountToPatient/link-account-to-patient.mjs index 35d7c027..9f30e41c 100644 --- a/sanitas_backend/src/handlers/LinkAccountToPatient/link-account-to-patient.mjs +++ b/sanitas_backend/src/handlers/LinkAccountToPatient/link-account-to-patient.mjs @@ -1,6 +1,6 @@ import { getPgClient, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT } from "utils/index.mjs"; +import { createResponse, decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * @type {import("src/commonTypes.mjs").AWSHandler} @@ -22,7 +22,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -41,7 +44,7 @@ export const handler = async (event, context) => { /** @type {import("src/commonTypes.mjs").LinkData} */ const { cui } = JSON.parse(event.body); - if (!email) { + if (!cui) { logger.error("No cui found!"); return responseBuilder .setStatusCode(400) @@ -100,7 +103,18 @@ export const handler = async (event, context) => { logger.info({ response }, "Responding with:"); return response; } catch (error) { - logger.error(error, "An error occurred while linking account!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while linking account!", + ); let statusCode = 500; let errorMessage = diff --git a/sanitas_backend/src/handlers/SearchPatient/search-patient.mjs b/sanitas_backend/src/handlers/SearchPatient/search-patient.mjs index 7aa78ef7..57474cd4 100644 --- a/sanitas_backend/src/handlers/SearchPatient/search-patient.mjs +++ b/sanitas_backend/src/handlers/SearchPatient/search-patient.mjs @@ -1,6 +1,6 @@ import { getPgClient, isDoctor, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT } from "utils/index.mjs"; +import { createResponse, decodeJWT, toSafeEvent } from "utils/index.mjs"; /** * @typedef {Object} RequestParams @@ -83,7 +83,10 @@ export const searchPatientHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -107,7 +110,7 @@ export const searchPatientHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) .setBody({ error: msg }) @@ -206,6 +209,10 @@ WHERE ( }); if (transactionResult.error) { + logger.error( + { err: transactionResult.error }, + "An error occurred during the database transaction!", + ); throw transactionResult.error; } @@ -223,7 +230,19 @@ WHERE ( return responseBuilder.setStatusCode(200).setBody(response.rows).build(); } catch (error) { - logger.error({ error: error.message }, "An error has occurred!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while searching patient!", + ); + return responseBuilder .setStatusCode(500) .setBody({ error: "DB Error" }) diff --git a/sanitas_backend/src/handlers/UpdateAllergicHistory/update-allergic-history-patient.mjs b/sanitas_backend/src/handlers/UpdateAllergicHistory/update-allergic-history-patient.mjs index 15836881..28c02c10 100644 --- a/sanitas_backend/src/handlers/UpdateAllergicHistory/update-allergic-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateAllergicHistory/update-allergic-history-patient.mjs @@ -1,7 +1,7 @@ import { getPgClient, isDoctor, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse, decodeJWT } from "utils"; -import { mapToAPIAllergicHistory } from "utils/index.mjs"; +import { mapToAPIAllergicHistory, toSafeEvent } from "utils/index.mjs"; /** * Handles the HTTP PUT request to update or create allergic history for a specific patient. @@ -26,7 +26,10 @@ export const updateAllergicHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -47,7 +50,7 @@ export const updateAllergicHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -130,6 +133,19 @@ export const updateAllergicHistoryHandler = async (event, context) => { } catch (error) { logger.error(error, "An error occurred while updating allergic history!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating allergic history!", + ); + if (error.code === "23503") { return responseBuilder .setStatusCode(404) diff --git a/sanitas_backend/src/handlers/UpdateCollaborator/update-collaborator.mjs b/sanitas_backend/src/handlers/UpdateCollaborator/update-collaborator.mjs index de188d4c..0e90e547 100644 --- a/sanitas_backend/src/handlers/UpdateCollaborator/update-collaborator.mjs +++ b/sanitas_backend/src/handlers/UpdateCollaborator/update-collaborator.mjs @@ -4,6 +4,7 @@ import { createResponse, decodeJWT, mapToAPICollaboratorInfo, + toSafeEvent, } from "utils/index.mjs"; export const updateCollaboratorHandler = async (event, context) => { @@ -24,7 +25,10 @@ export const updateCollaboratorHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -49,7 +53,7 @@ export const updateCollaboratorHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -123,7 +127,18 @@ export const updateCollaboratorHandler = async (event, context) => { .setBody(apiUpdatedCollaborator) .build(); } catch (error) { - logger.error({ error }, "Error querying database:"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating collaborator!", + ); if (error.code === "23503") { return responseBuilder diff --git a/sanitas_backend/src/handlers/UpdateConsultationPatient/update-consultation-patient.mjs b/sanitas_backend/src/handlers/UpdateConsultationPatient/update-consultation-patient.mjs index f48397bf..f604e063 100644 --- a/sanitas_backend/src/handlers/UpdateConsultationPatient/update-consultation-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateConsultationPatient/update-consultation-patient.mjs @@ -1,7 +1,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse } from "utils"; -import { decodeJWT, mapToAPIMedicalConsultation } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIMedicalConsultation, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP PUT request to update or create a medical consultation for a specific patient. @@ -26,7 +30,10 @@ export const updateMedicalConsultationHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -59,7 +66,7 @@ export const updateMedicalConsultationHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error occurred while trying to check if the user is a doctor!"; - logger.error(itsDoctor, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); const response = responseBuilder .setStatusCode(500) @@ -193,8 +200,16 @@ export const updateMedicalConsultationHandler = async (event, context) => { }) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { details: error.details, error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating medical consultation!", ); @@ -215,8 +230,6 @@ export const updateMedicalConsultationHandler = async (event, context) => { }) .build(); } finally { - if (client) { - await client.end(); - } + await client?.end(); } }; diff --git a/sanitas_backend/src/handlers/UpdateFamilyHistory/update-family-history-patient.mjs b/sanitas_backend/src/handlers/UpdateFamilyHistory/update-family-history-patient.mjs index 19c8552a..bbf47270 100644 --- a/sanitas_backend/src/handlers/UpdateFamilyHistory/update-family-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateFamilyHistory/update-family-history-patient.mjs @@ -4,6 +4,7 @@ import { decodeJWT, createResponse, mapToAPIFamilyHistory, + toSafeEvent, } from "utils/index.mjs"; /** @@ -29,7 +30,10 @@ export const updateFamilyHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -49,7 +53,7 @@ export const updateFamilyHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -118,7 +122,18 @@ export const updateFamilyHistoryHandler = async (event, context) => { .setBody(mapToAPIFamilyHistory(updatedRecord)) .build(); } catch (error) { - logger.error({ error }, "An error occurred while updating family history!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating family history!", + ); if (error.code === "23503") { return responseBuilder diff --git a/sanitas_backend/src/handlers/UpdateGynecoObstetricHistory/update-gynecoobstetric-history-patient.mjs b/sanitas_backend/src/handlers/UpdateGynecoObstetricHistory/update-gynecoobstetric-history-patient.mjs index 07540758..2cbc4595 100644 --- a/sanitas_backend/src/handlers/UpdateGynecoObstetricHistory/update-gynecoobstetric-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateGynecoObstetricHistory/update-gynecoobstetric-history-patient.mjs @@ -4,6 +4,7 @@ import { createResponse, decodeJWT, mapToAPIGynecologicalHistory, + toSafeEvent, } from "utils/index.mjs"; export const updateGynecologicalHistoryHandler = async (event, context) => { @@ -23,7 +24,10 @@ export const updateGynecologicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -44,7 +48,7 @@ export const updateGynecologicalHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -174,10 +178,19 @@ export const updateGynecologicalHistoryHandler = async (event, context) => { .setBody(formattedResponse) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating gynecological history!", ); + if (error.code === "23503") { return responseBuilder .setStatusCode(404) diff --git a/sanitas_backend/src/handlers/UpdateNonPatologicalHistory/index.mjs b/sanitas_backend/src/handlers/UpdateNonPatologicalHistory/index.mjs index 769308ab..9ca6de30 100644 --- a/sanitas_backend/src/handlers/UpdateNonPatologicalHistory/index.mjs +++ b/sanitas_backend/src/handlers/UpdateNonPatologicalHistory/index.mjs @@ -4,6 +4,7 @@ import { decodeJWT, createResponse, mapToAPINonPathologicalHistory, + toSafeEvent, } from "utils/index.mjs"; export const handler = async (event, context) => { @@ -23,7 +24,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -43,7 +47,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -99,10 +103,19 @@ export const handler = async (event, context) => { .setBody(formattedResponse) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating non-pathological history!", ); + if (error.code === "23503") { return responseBuilder .setStatusCode(404) diff --git a/sanitas_backend/src/handlers/UpdatePatient/update-patient.mjs b/sanitas_backend/src/handlers/UpdatePatient/update-patient.mjs index 84d96965..9f79b53d 100644 --- a/sanitas_backend/src/handlers/UpdatePatient/update-patient.mjs +++ b/sanitas_backend/src/handlers/UpdatePatient/update-patient.mjs @@ -1,6 +1,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT, mapToAPIPatient } from "utils/index.mjs"; +import { + createResponse, + decodeJWT, + mapToAPIPatient, + toSafeEvent, +} from "utils/index.mjs"; // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: The function isn't that complex, it's just really large. export const updatePatientHandler = async (event, context) => { @@ -19,7 +24,10 @@ export const updatePatientHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -43,7 +51,7 @@ export const updatePatientHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -133,12 +141,20 @@ export const updatePatientHandler = async (event, context) => { logger.info(response, "Respondiendo con:"); return response; } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - error, - "¡Se produjo un error al actualizar los datos del paciente!", + { err: errorDetails, event: safeEvent }, + "An error occurred while updating patient data!", ); - return responseBuilder.setStatusCode(400).setBody({ + return responseBuilder.setStatusCode(500).setBody({ error: "Se produjo un error al actualizar los datos del paciente.", }); } finally { diff --git a/sanitas_backend/src/handlers/UpdatePersonalHistory/update-personal-history-patient.mjs b/sanitas_backend/src/handlers/UpdatePersonalHistory/update-personal-history-patient.mjs index 37e6e36e..cffb6431 100644 --- a/sanitas_backend/src/handlers/UpdatePersonalHistory/update-personal-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdatePersonalHistory/update-personal-history-patient.mjs @@ -1,7 +1,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse } from "utils"; -import { decodeJWT, mapToAPIPersonalHistory } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIPersonalHistory, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP PUT request to update or create personal medical history for a specific patient. @@ -26,7 +30,10 @@ export const updatePersonalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -46,7 +53,7 @@ export const updatePersonalHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -115,8 +122,16 @@ export const updatePersonalHistoryHandler = async (event, context) => { .setBody(mapToAPIPersonalHistory(updatedRecord)) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating personal history!", ); diff --git a/sanitas_backend/src/handlers/UpdatePsychiatricHistory/update-psychiatric-history-patient.mjs b/sanitas_backend/src/handlers/UpdatePsychiatricHistory/update-psychiatric-history-patient.mjs index 1cb1a3e0..bc5f9ed0 100644 --- a/sanitas_backend/src/handlers/UpdatePsychiatricHistory/update-psychiatric-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdatePsychiatricHistory/update-psychiatric-history-patient.mjs @@ -1,7 +1,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse } from "utils"; -import { decodeJWT, mapToAPIPsychiatricHistory } from "utils/index.mjs"; +import { + decodeJWT, + mapToAPIPsychiatricHistory, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP PUT request to update or create psychiatric history for a specific patient. @@ -26,7 +30,10 @@ export const updatePsychiatricHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -46,7 +53,7 @@ export const updatePsychiatricHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -125,8 +132,16 @@ export const updatePsychiatricHistoryHandler = async (event, context) => { .setBody(mapToAPIPsychiatricHistory(updatedRecord)) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating psychiatric history!", ); diff --git a/sanitas_backend/src/handlers/UpdateStudentAllergicHistory/update-student-allergic-history-patient.mjs b/sanitas_backend/src/handlers/UpdateStudentAllergicHistory/update-student-allergic-history-patient.mjs index 78be3cf8..f35c59ea 100644 --- a/sanitas_backend/src/handlers/UpdateStudentAllergicHistory/update-student-allergic-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentAllergicHistory/update-student-allergic-history-patient.mjs @@ -6,6 +6,7 @@ import { decodeJWT, mapToAPIAllergicHistory, requestIsSubset, + toSafeEvent, } from "utils/index.mjs"; /** @@ -32,7 +33,10 @@ export const updateStudentAllergicHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -56,7 +60,7 @@ export const updateStudentAllergicHistoryHandler = async (event, context) => { if (itsDoctor.error) { const msg = "An error ocurred while trying to check if the user is a doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -99,7 +103,10 @@ export const updateStudentAllergicHistoryHandler = async (event, context) => { "Comparing medicalHistory with existingData...", ); if (requestModifiesSavedData(medicalHistory, existingData)) { - logger.error("Request data modifies saved data!"); + logger.error( + { request: medicalHistory, DB: existingData }, + "Request data modifies saved data!", + ); const response = responseBuilder .setStatusCode(403) .setBody({ @@ -186,7 +193,18 @@ export const updateStudentAllergicHistoryHandler = async (event, context) => { logger.info({ response }, "Done! Responding with:"); return response; } catch (error) { - logger.error(error, "An error occurred while updating allergic history!"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating allergic history!", + ); let statusCode = 500; let errorMessage = diff --git a/sanitas_backend/src/handlers/UpdateStudentCollaboratorInformation/update-student-collaborator-information.mjs b/sanitas_backend/src/handlers/UpdateStudentCollaboratorInformation/update-student-collaborator-information.mjs index eb43a335..c39fbd0a 100644 --- a/sanitas_backend/src/handlers/UpdateStudentCollaboratorInformation/update-student-collaborator-information.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentCollaboratorInformation/update-student-collaborator-information.mjs @@ -4,6 +4,7 @@ import { createResponse, decodeJWT, mapToAPICollaboratorInfo, + toSafeEvent, } from "utils/index.mjs"; /** @@ -38,7 +39,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -64,7 +68,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -114,7 +118,10 @@ export const handler = async (event, context) => { "Checking if reqData modifies dbData...", ); if (patientAlreadyRegistered && modifiesData(dbData, collaboratorData)) { - logger.error("Request modifies saved data!"); + logger.error( + { DB: dbData, request: collaboratorData }, + "Request modifies saved data!", + ); const body = { error: "Unauthorized to modify data!" }; const response = responseBuilder .setStatusCode(403) @@ -179,7 +186,19 @@ export const handler = async (event, context) => { .setBody(apiUpdatedCollaborator) .build(); } catch (error) { - logger.error({ error }, "Error querying database:"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating collaborator information!", + ); + let msg = "Internal Server Error"; let code = 500; diff --git a/sanitas_backend/src/handlers/UpdateStudentData/update-student-data.mjs b/sanitas_backend/src/handlers/UpdateStudentData/update-student-data.mjs index 6b994b5d..c40ea115 100644 --- a/sanitas_backend/src/handlers/UpdateStudentData/update-student-data.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentData/update-student-data.mjs @@ -1,6 +1,10 @@ import { getPgClient, SCHEMA_NAME } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, mapToAPIStudentInfo } from "utils/index.mjs"; +import { + createResponse, + mapToAPIStudentInfo, + toSafeEvent, +} from "utils/index.mjs"; /** * @param {import('aws-lambda').APIGatewayProxyEvent} event @@ -71,7 +75,18 @@ export const handler = async (event, context) => { .setBody(mapToAPIStudentInfo(studentData)) .build(); } catch (error) { - logger.error({ error }, "Error querying database:"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating student data!", + ); if (error.code === "23503") { logger.error("A patient with the given ID doesn't exists!"); diff --git a/sanitas_backend/src/handlers/UpdateStudentFamiliarHistory/update-student-familiar-history-patient.mjs b/sanitas_backend/src/handlers/UpdateStudentFamiliarHistory/update-student-familiar-history-patient.mjs index ffaeaad1..d21e0395 100644 --- a/sanitas_backend/src/handlers/UpdateStudentFamiliarHistory/update-student-familiar-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentFamiliarHistory/update-student-familiar-history-patient.mjs @@ -6,6 +6,7 @@ import { createResponse, mapToAPIFamilyHistory, requestIsSubset, + toSafeEvent, } from "utils/index.mjs"; /** @@ -34,7 +35,10 @@ export const updateStudentFamilyHistoryHandler = async (event, context) => { logger.info({ tokenPayload: tokenInfo }, "Decoded JWT payload"); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -56,7 +60,7 @@ export const updateStudentFamilyHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -190,7 +194,19 @@ export const updateStudentFamilyHistoryHandler = async (event, context) => { .build(); } catch (error) { await client.query("rollback"); - logger.error({ error }, "An error occurred while updating family history!"); + + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating family history!", + ); if (error.code === "23503") { return responseBuilder diff --git a/sanitas_backend/src/handlers/UpdateStudentGeneralInformation/update-student-general-information.mjs b/sanitas_backend/src/handlers/UpdateStudentGeneralInformation/update-student-general-information.mjs index a5325d5c..63ad0393 100644 --- a/sanitas_backend/src/handlers/UpdateStudentGeneralInformation/update-student-general-information.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentGeneralInformation/update-student-general-information.mjs @@ -1,6 +1,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; -import { createResponse, decodeJWT, mapToAPIPatient } from "utils/index.mjs"; +import { + createResponse, + decodeJWT, + mapToAPIPatient, + toSafeEvent, +} from "utils/index.mjs"; /** * Checks if the given `requestData` modifies values from the `dbData`. @@ -82,7 +87,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -103,7 +111,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -248,8 +256,16 @@ export const handler = async (event, context) => { logger.info({ response }, "Done! Responding with:"); return response; } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating surgical history!", ); diff --git a/sanitas_backend/src/handlers/UpdateStudentGynecoObstetricHistory/update-student-gyneco-obstetric-history.mjs b/sanitas_backend/src/handlers/UpdateStudentGynecoObstetricHistory/update-student-gyneco-obstetric-history.mjs index 71da5208..6e4e6b36 100644 --- a/sanitas_backend/src/handlers/UpdateStudentGynecoObstetricHistory/update-student-gyneco-obstetric-history.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentGynecoObstetricHistory/update-student-gyneco-obstetric-history.mjs @@ -5,6 +5,7 @@ import { decodeJWT, mapToAPIGynecologicalHistory, requestIsSubset, + toSafeEvent, } from "utils/index.mjs"; /** @@ -209,7 +210,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -230,7 +234,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -413,8 +417,16 @@ export const handler = async (event, context) => { logger.info({ response }, "Responding with:"); return response; } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating gynecological history!", ); diff --git a/sanitas_backend/src/handlers/UpdateStudentNonPatologicalHistory/index.mjs b/sanitas_backend/src/handlers/UpdateStudentNonPatologicalHistory/index.mjs index ac5a7799..19803b2f 100644 --- a/sanitas_backend/src/handlers/UpdateStudentNonPatologicalHistory/index.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentNonPatologicalHistory/index.mjs @@ -5,6 +5,7 @@ import { createResponse, mapToAPINonPathologicalHistory, checkForUnauthorizedChangesPathological, + toSafeEvent, } from "utils/index.mjs"; export const handler = async (event, context) => { @@ -24,7 +25,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -44,7 +48,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } if (itsDoctor) { @@ -136,10 +140,19 @@ export const handler = async (event, context) => { .setBody(formattedResponse) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating non-pathological history!", ); + if (error.code === "23503") { return responseBuilder .setStatusCode(404) diff --git a/sanitas_backend/src/handlers/UpdateStudentPersonalHistory/update-student-personal-history-patient.mjs b/sanitas_backend/src/handlers/UpdateStudentPersonalHistory/update-student-personal-history-patient.mjs index 611d2476..ae80f694 100644 --- a/sanitas_backend/src/handlers/UpdateStudentPersonalHistory/update-student-personal-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentPersonalHistory/update-student-personal-history-patient.mjs @@ -6,6 +6,7 @@ import { createResponse, mapToAPIPersonalHistory, requestIsSubset, + toSafeEvent, } from "utils/index.mjs"; /** @@ -34,7 +35,10 @@ export const updateStudentPersonalHistoryHandler = async (event, context) => { logger.info({ tokenPayload: tokenInfo }, "Decoded JWT payload"); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -93,7 +97,10 @@ export const updateStudentPersonalHistoryHandler = async (event, context) => { "Comparing medicalHistory with existingData...", ); if (requestModifiesSavedData(medicalHistory, existingData)) { - logger.error("Request modifies data!"); + logger.error( + { DB: existingData, request: medicalHistory }, + "Request modifies data!", + ); const response = responseBuilder .setStatusCode(403) .setBody({ error: "Not authorized to update data!" }) @@ -184,8 +191,17 @@ export const updateStudentPersonalHistoryHandler = async (event, context) => { .build(); } catch (error) { await client.query("rollback"); + + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating personal history!", ); diff --git a/sanitas_backend/src/handlers/UpdateStudentPsychiatricHistory/update-student-psychiatric-history-patient.mjs b/sanitas_backend/src/handlers/UpdateStudentPsychiatricHistory/update-student-psychiatric-history-patient.mjs index 26c4efe1..87b4adfd 100644 --- a/sanitas_backend/src/handlers/UpdateStudentPsychiatricHistory/update-student-psychiatric-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentPsychiatricHistory/update-student-psychiatric-history-patient.mjs @@ -6,6 +6,7 @@ import { decodeJWT, mapToAPIPsychiatricHistory, checkForUnauthorizedChanges, + toSafeEvent, } from "utils/index.mjs"; function extractOldData(dbData) { @@ -56,7 +57,10 @@ export const updateStudentPsychiatricHistoryHandler = async ( logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -76,7 +80,7 @@ export const updateStudentPsychiatricHistoryHandler = async ( const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -203,8 +207,17 @@ export const updateStudentPsychiatricHistoryHandler = async ( .build(); } catch (error) { await client.query("rollback"); + + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating psychiatric history!", ); @@ -223,8 +236,6 @@ export const updateStudentPsychiatricHistoryHandler = async ( }) .build(); } finally { - if (client) { - await client.end(); - } + await client?.end(); } }; diff --git a/sanitas_backend/src/handlers/UpdateStudentStudentInformation/update-student-student-information.mjs b/sanitas_backend/src/handlers/UpdateStudentStudentInformation/update-student-student-information.mjs index 089dde56..49bdc849 100644 --- a/sanitas_backend/src/handlers/UpdateStudentStudentInformation/update-student-student-information.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentStudentInformation/update-student-student-information.mjs @@ -4,6 +4,7 @@ import { createResponse, decodeJWT, mapToAPIStudentInfo, + toSafeEvent, } from "utils/index.mjs"; /** @@ -44,7 +45,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -88,7 +92,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -113,7 +117,10 @@ export const handler = async (event, context) => { : null; if (foundData && requestUpdatesValues(dbData, requestData)) { - logger.error({ dbData, requestData }, "requestData modifies dbData!"); + logger.error( + { DB: dbData, request: requestData }, + "requestData modifies dbData!", + ); const response = responseBuilder .setStatusCode(403) @@ -157,7 +164,18 @@ export const handler = async (event, context) => { return responseBuilder.setStatusCode(200).setBody(studentData).build(); } catch (error) { - logger.error({ error }, "Error querying database:"); + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + + logger.error( + { err: errorDetails, event: safeEvent }, + "An error occurred while updating student information!", + ); if (error.code === "23503") { logger.error("A patient with the given ID doesn't exists!"); @@ -175,13 +193,11 @@ export const handler = async (event, context) => { .build(); } - logger.error(error, "An error occurred!"); return responseBuilder .setStatusCode(500) .setBody({ error: "An internal error ocurred" }) .build(); } finally { await client?.end(); - logger.info("Database connection closed!"); } }; diff --git a/sanitas_backend/src/handlers/UpdateStudentSurgicalHistory/update-student-surgical-history-patient.mjs b/sanitas_backend/src/handlers/UpdateStudentSurgicalHistory/update-student-surgical-history-patient.mjs index f6272925..5f2e7faa 100644 --- a/sanitas_backend/src/handlers/UpdateStudentSurgicalHistory/update-student-surgical-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentSurgicalHistory/update-student-surgical-history-patient.mjs @@ -5,6 +5,7 @@ import { decodeJWT, mapToAPISurgicalHistory, requestDataEditsDBData, + toSafeEvent, } from "utils/index.mjs"; /** @@ -30,7 +31,10 @@ export const updateStudentSurgicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -51,7 +55,7 @@ export const updateStudentSurgicalHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -151,8 +155,17 @@ export const updateStudentSurgicalHistoryHandler = async (event, context) => { .build(); } catch (error) { await client.query("rollback"); + + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating surgical history!", ); @@ -171,8 +184,6 @@ export const updateStudentSurgicalHistoryHandler = async (event, context) => { }) .build(); } finally { - if (client) { - await client.end(); - } + await client?.end(); } }; diff --git a/sanitas_backend/src/handlers/UpdateStudentTraumatologicalHistory/index.mjs b/sanitas_backend/src/handlers/UpdateStudentTraumatologicalHistory/index.mjs index 575e0077..972e4b22 100644 --- a/sanitas_backend/src/handlers/UpdateStudentTraumatologicalHistory/index.mjs +++ b/sanitas_backend/src/handlers/UpdateStudentTraumatologicalHistory/index.mjs @@ -2,7 +2,11 @@ import { getPgClient, isDoctor, SCHEMA_NAME, transaction } from "db-conn"; import { logger, withRequest } from "logging"; import { createResponse } from "utils"; import { mapToAPITraumatologicHistory } from "utils"; -import { decodeJWT, requestDataEditsDBData } from "utils/index.mjs"; +import { + decodeJWT, + requestDataEditsDBData, + toSafeEvent, +} from "utils/index.mjs"; /** * Handles the HTTP POST request to update or create the traumatologic history for a specific patient. @@ -27,7 +31,10 @@ export const handler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -47,7 +54,7 @@ export const handler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -155,8 +162,16 @@ export const handler = async (event, context) => { .setBody(formattedResponse) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating traumatologic history!", ); diff --git a/sanitas_backend/src/handlers/UpdateSurgicalHistory/update-surgical-history-patient.mjs b/sanitas_backend/src/handlers/UpdateSurgicalHistory/update-surgical-history-patient.mjs index 9a98c56e..00a99c44 100644 --- a/sanitas_backend/src/handlers/UpdateSurgicalHistory/update-surgical-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateSurgicalHistory/update-surgical-history-patient.mjs @@ -4,6 +4,7 @@ import { createResponse, decodeJWT, mapToAPISurgicalHistory, + toSafeEvent, } from "utils/index.mjs"; /** @@ -29,7 +30,10 @@ export const updateSurgicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -49,7 +53,7 @@ export const updateSurgicalHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -104,8 +108,16 @@ export const updateSurgicalHistoryHandler = async (event, context) => { .setBody(formattedResponse) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = toSafeEvent(event); + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating surgical history!", ); diff --git a/sanitas_backend/src/handlers/UpdateTraumatologicalHistory/update-traumatological-history-patient.mjs b/sanitas_backend/src/handlers/UpdateTraumatologicalHistory/update-traumatological-history-patient.mjs index c43a8249..a4957ffd 100644 --- a/sanitas_backend/src/handlers/UpdateTraumatologicalHistory/update-traumatological-history-patient.mjs +++ b/sanitas_backend/src/handlers/UpdateTraumatologicalHistory/update-traumatological-history-patient.mjs @@ -27,7 +27,10 @@ export const updateTraumatologicalHistoryHandler = async (event, context) => { logger.info({ jwt }, "Parsing JWT..."); const tokenInfo = decodeJWT(jwt); if (tokenInfo.error) { - logger.error({ error: tokenInfo.error }, "JWT couldn't be parsed!"); + logger.error( + { err: tokenInfo.error, inputs: { jwt } }, + "JWT couldn't be parsed!", + ); return responseBuilder .setStatusCode(400) .setBody({ error: "JWT couldn't be parsed" }) @@ -47,7 +50,7 @@ export const updateTraumatologicalHistoryHandler = async (event, context) => { const itsDoctor = await isDoctor(client, email); if (itsDoctor.error) { const msg = "An error occurred while trying to check if user is doctor!"; - logger.error({ error: itsDoctor.error }, msg); + logger.error({ err: itsDoctor.error, inputs: { email } }, msg); return responseBuilder.setStatusCode(500).setBody({ error: msg }).build(); } @@ -100,8 +103,24 @@ export const updateTraumatologicalHistoryHandler = async (event, context) => { .setBody(formattedResponse) .build(); } catch (error) { + const errorDetails = { + message: error.message, + stack: error.stack, + type: error.constructor.name, + }; + + const safeEvent = { + httpMethod: event.httpMethod, + path: event.path, + headers: { + ...event.headers, + Authorization: "[REDACTED]", + }, + body: event.body ? JSON.stringify(event.body) : "No body", + }; + logger.error( - { error }, + { err: errorDetails, event: safeEvent }, "An error occurred while updating traumatologic history!", ); diff --git a/sanitas_frontend/src/components/StudentDashboardTopBar/index.jsx b/sanitas_frontend/src/components/StudentDashboardTopBar/index.jsx index 4c1fc46e..8a6fe85b 100644 --- a/sanitas_frontend/src/components/StudentDashboardTopBar/index.jsx +++ b/sanitas_frontend/src/components/StudentDashboardTopBar/index.jsx @@ -3,8 +3,6 @@ import { useNavigate } from "react-router-dom"; import { colors } from "src/theme.mjs"; import arrowLeft from "@tabler/icons/outline/arrow-narrow-left.svg"; import arrowRight from "@tabler/icons/outline/arrow-narrow-right.svg"; -import IconButton from "src/components/Button/Icon"; -import logoutIcon from "@tabler/icons/outline/door-exit.svg"; /** * @callback NavigationHandler @@ -48,9 +46,7 @@ export default function StudentDashboardTopbar({ navigateToPsiquiatricStudent, navigateToSurgicalStudent, navigateToTraumatologicalStudent, - navigateToLogin, useStore, - logoutUser, }) { const navigate = useNavigate(); const [activeSection, setActiveSection] = useState(activeSectionProp); @@ -208,19 +204,6 @@ export default function StudentDashboardTopbar({ }} style={{ cursor: "pointer" }} /> - { - logoutUser(); - navigateToLogin()(navigate); - }} - style={{ - position: "absolute", - right: "0", - marginRight: "3rem", - marginTop: "12rem", - }} - /> ); } diff --git a/sanitas_frontend/src/views/UpdatePatientGeneralInformationView/index.jsx b/sanitas_frontend/src/views/UpdatePatientGeneralInformationView/index.jsx index 3b499b95..4e51a6a3 100644 --- a/sanitas_frontend/src/views/UpdatePatientGeneralInformationView/index.jsx +++ b/sanitas_frontend/src/views/UpdatePatientGeneralInformationView/index.jsx @@ -1,4 +1,5 @@ import { Suspense, useMemo, useState, useEffect } from "react"; +import { useNavigate } from "react-router-dom"; import "react-toastify/dist/ReactToastify.css"; import { toast } from "react-toastify"; import DropdownMenu from "src/components/DropdownMenu"; @@ -12,6 +13,8 @@ import StudentDashboardTopbar from "src/components/StudentDashboardTopBar"; import { createRefreshSignal } from "src/utils/refreshHook"; import useWindowSize from "src/utils/useWindowSize"; import BaseButton from "src/components/Button/Base"; +import IconButton from "src/components/Button/Icon"; +import logoutIcon from "@tabler/icons/outline/door-exit.svg"; /** * Checks if the given property exists and is not a null value inside the object. @@ -71,6 +74,8 @@ export default function UpdatePatientInfoView({ getCollaboratorInformation, updateCollaboratorInformation, }) { + const navigate = useNavigate(); + const setIsWoman = useStore((s) => s.setIsWoman); const id = useStore((s) => s.selectedPatientId); @@ -130,6 +135,15 @@ export default function UpdatePatientInfoView({ flexGrow: 1, }} > +
+ { + sidebarConfig.logoutUser(); + sidebarConfig.navigateToLogin()(navigate); + }} + /> +
} > @@ -139,7 +153,8 @@ export default function UpdatePatientInfoView({ fontFamily: fonts.titleFont, fontSize: fontSize.titleSize, textAlign: "center", - padding: "2rem 0 0.8rem 0", + padding: "0 0 0.8rem 0", + marginTop: isMobile ? "0rem" : "-3.15rem", //La scrum master me dijo que lo pusiera así }} > Datos Generales