From 653d2128e9b0119df734347eb46cc73861c2159d Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Mon, 3 Feb 2025 12:06:28 +0100
Subject: [PATCH] fix: gracefully handle error in insight processing

---
 src/readers/insightProcessor.ts | 100 +++++++++++++++++---------------
 1 file changed, 52 insertions(+), 48 deletions(-)

diff --git a/src/readers/insightProcessor.ts b/src/readers/insightProcessor.ts
index 29b5bf3..10c7068 100644
--- a/src/readers/insightProcessor.ts
+++ b/src/readers/insightProcessor.ts
@@ -90,55 +90,59 @@ export const insightProcessor = async () => {
     const convertedQuery = Query2BackendQuery(ss.id, visualQuery, queryBuilderSettings, []);
     const query = query2Cypher(convertedQuery);
     if (query == null) return;
-    const result = await queryService(ss.dbConnections[0], query);
-
-    insight.status = false;
-
-    if (insight.alarmMode === 'always') {
-      insight.status = true;
-    } else if (insight.alarmMode === 'diff') {
-      insight = await diffCheck(insight, ss, result);
-    } else if (insight.alarmMode === 'conditional' && insight.conditionsCheck && insight.conditionsCheck.length > 0) {
-      insight = statCheck(insight, result);
-    }
-
-    if (insight.userId == null) return; // fixes ts but never is the case
-    await ums.updateInsight(insight.userId, insight.id, insight);
-
-    if (insight.status || message.force) {
-      if (insight.status) log.debug('Insight passed the check');
-      if (message.force) log.debug('Forced insight processing');
-
-      editor.read(async () => {
-        const cleanUpDom = setUpDom();
-        let html = $generateHtmlFromNodes(editor);
-        cleanUpDom();
-
-        html = await populateTemplate(html, result, visualizations);
-
-        for (const recipient of insight.recipients) {
-          if (mail == null) {
-            log.warn('Mail is not configured. Insight processor will be disabled');
-            return;
+    try {
+      const result = await queryService(ss.dbConnections[0], query);
+
+      insight.status = false;
+
+      if (insight.alarmMode === 'always') {
+        insight.status = true;
+      } else if (insight.alarmMode === 'diff') {
+        insight = await diffCheck(insight, ss, result);
+      } else if (insight.alarmMode === 'conditional' && insight.conditionsCheck && insight.conditionsCheck.length > 0) {
+        insight = statCheck(insight, result);
+      }
+
+      if (insight.userId == null) return; // fixes ts but never is the case
+      await ums.updateInsight(insight.userId, insight.id, insight);
+
+      if (insight.status || message.force) {
+        if (insight.status) log.debug('Insight passed the check');
+        if (message.force) log.debug('Forced insight processing');
+
+        editor.read(async () => {
+          const cleanUpDom = setUpDom();
+          let html = $generateHtmlFromNodes(editor);
+          cleanUpDom();
+
+          html = await populateTemplate(html, result, visualizations);
+
+          for (const recipient of insight.recipients) {
+            if (mail == null) {
+              log.warn('Mail is not configured. Insight processor will be disabled');
+              return;
+            }
+
+            if (DEBUG_EMAIL) {
+              log.warn('DEBUG: Would have sent mail to', recipient);
+              continue;
+            }
+
+            log.debug('Sending mail to', recipient);
+            await mail.sendMail({
+              to: recipient,
+              from: SMTP_USER,
+              subject: `GraphPolaris report: ${insight.name}`,
+              html: html,
+            });
+            log.info('Mail sent to ', recipient);
           }
-
-          if (DEBUG_EMAIL) {
-            log.warn('DEBUG: Would have sent mail to', recipient);
-            continue;
-          }
-
-          log.debug('Sending mail to', recipient);
-          await mail.sendMail({
-            to: recipient,
-            from: SMTP_USER,
-            subject: `GraphPolaris report: ${insight.name}`,
-            html: html,
-          });
-          log.info('Mail sent to ', recipient);
-        }
-      });
-    } else {
-      log.debug('WARN: Insight did not pass the check');
+        });
+      } else {
+        log.debug('WARN: Insight did not pass the check');
+      }
+    } catch (err) {
+      log.error('Error processing insight', err);
     }
   });
 };
-- 
GitLab