refactor: Improve error handling in CheckHeartbeat.ts

This commit improves the error handling in the CheckHeartbeat.ts file. It adds a try-catch block around the main logic to catch any errors that occur during the processing of incoming request monitors. Additionally, it logs the error message and the ID of the monitor that caused the error. This change enhances the robustness and reliability of the code by properly handling and logging errors.
This commit is contained in:
Simon Larsen 2024-06-24 18:50:59 +01:00
parent 09a7e155b7
commit 9348076df0
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA

View File

@ -41,32 +41,39 @@ RunCron(
logger.debug(incomingRequestMonitors);
for (const monitor of incomingRequestMonitors) {
if (!monitor.monitorSteps) {
logger.debug("Monitor has no steps. Skipping...");
continue;
try {
if (!monitor.monitorSteps) {
logger.debug("Monitor has no steps. Skipping...");
continue;
}
const processRequest: boolean = shouldProcessRequest(monitor);
logger.debug(
`Monitor: ${monitor.id} should process request: ${processRequest}`,
);
if (!processRequest) {
continue;
}
const incomingRequest: IncomingMonitorRequest = {
monitorId: monitor.id!,
requestHeaders: undefined,
requestBody: undefined,
requestMethod: undefined,
incomingRequestReceivedAt:
monitor.incomingRequestReceivedAt || monitor.createdAt!,
onlyCheckForIncomingRequestReceivedAt: true,
};
await ProbeMonitorResponseService.processProbeResponse(incomingRequest);
} catch (error) {
logger.error(
`Error while processing incoming request monitor: ${monitor.id?.toString()}`,
);
logger.error(error);
}
const processRequest: boolean = shouldProcessRequest(monitor);
logger.debug(
`Monitor: ${monitor.id} should process request: ${processRequest}`,
);
if (!processRequest) {
continue;
}
const incomingRequest: IncomingMonitorRequest = {
monitorId: monitor.id!,
requestHeaders: undefined,
requestBody: undefined,
requestMethod: undefined,
incomingRequestReceivedAt:
monitor.incomingRequestReceivedAt || monitor.createdAt!,
onlyCheckForIncomingRequestReceivedAt: true,
};
await ProbeMonitorResponseService.processProbeResponse(incomingRequest);
}
},
);