From 9348076df0a41e86042dc96998c248317718f395 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 24 Jun 2024 18:50:59 +0100 Subject: [PATCH] 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. --- .../IncomingRequestMonitor/CheckHeartbeat.ts | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/App/FeatureSet/Workers/Jobs/IncomingRequestMonitor/CheckHeartbeat.ts b/App/FeatureSet/Workers/Jobs/IncomingRequestMonitor/CheckHeartbeat.ts index e3af09d499..5cccfd9afd 100644 --- a/App/FeatureSet/Workers/Jobs/IncomingRequestMonitor/CheckHeartbeat.ts +++ b/App/FeatureSet/Workers/Jobs/IncomingRequestMonitor/CheckHeartbeat.ts @@ -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); } }, );