Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: now logs show details and the inputs that caused the error #302

Merged
merged 10 commits into from
Nov 5, 2024
XavierLopez25 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions sanitas_backend/src/handlers/ExportData/export-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -172,7 +179,7 @@ export const handler = async (event, context) => {
return responseBuilder.setStatusCode(200).setBody(csv).build();
} catch (error) {
logger.error(
{ details: error.message },
{ err: error, details: error.message },
"An error occurred while exporting data!",
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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" })
Expand All @@ -50,7 +53,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!" })
Expand All @@ -69,7 +75,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)
Expand All @@ -89,7 +95,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)
Expand Down Expand Up @@ -127,6 +136,11 @@ export const getAllergicHistoryHandler = async (event, context) => {
});

if (transactionResult.error) {
logger.error(
{ err: transactionResult.error },
"An error occurred during the database transaction!",
);

throw transactionResult.error;
}

Expand All @@ -152,7 +166,7 @@ export const getAllergicHistoryHandler = async (event, context) => {
return responseBuilder.setStatusCode(200).setBody(medicalHistory).build();
} catch (error) {
logger.error(
{ error },
{ err: error },
XavierLopez25 marked this conversation as resolved.
Show resolved Hide resolved
"An error occurred while fetching allergic history!",
);
return responseBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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" })
Expand All @@ -42,7 +45,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!" })
Expand All @@ -63,7 +69,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)
Expand Down Expand Up @@ -100,6 +106,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;
}

Expand Down Expand Up @@ -127,8 +137,8 @@ export const getMedicalConsultationHandler = async (event, context) => {
.build();
} catch (error) {
logger.error(
{ err: error },
"An error occurred while fetching medical consultation!",
error,
);
return responseBuilder
.setStatusCode(500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand All @@ -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!" })
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -149,7 +162,7 @@ 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);
logger.error(error, "An error occurred while fetching family history!");
return responseBuilder
.setStatusCode(500)
.setBody({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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" })
Expand All @@ -43,7 +46,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!" })
Expand All @@ -66,7 +69,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)
Expand All @@ -85,7 +88,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)
Expand Down Expand Up @@ -118,6 +124,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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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" })
Expand All @@ -40,7 +43,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({
Expand All @@ -65,7 +68,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)
Expand All @@ -84,7 +87,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)
Expand Down Expand Up @@ -117,6 +123,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;
}

Expand Down Expand Up @@ -147,7 +157,7 @@ export const handler = async (event, context) => {
logger.info({ response }, "Responding with:");
return response;
} catch (error) {
logger.error({ error }, "An error has ocurred!");
logger.error(error, "An error has ocurred!");
const response = responseBuilder.setStatusCode(500).setBody(error).build();
return response;
} finally {
Expand Down
Loading