Sfoglia il codice sorgente

feat: store http request response in workflow log

Ahmad Kholid 2 anni fa
parent
commit
d2536f0676

+ 1 - 0
src/workflowEngine/WorkflowEngine.js

@@ -323,6 +323,7 @@ class WorkflowEngine {
           prevBlockData: detail.prevBlockData || '',
         },
         replacedValue: cloneDeep(detail.replacedValue),
+        ...(detail?.ctxData || {}),
       };
 
       delete detail.replacedValue;

+ 10 - 5
src/workflowEngine/WorkflowWorker.js

@@ -256,6 +256,7 @@ class WorkflowWorker {
 
         addBlockLog(result.status || 'success', {
           logId: result.logId,
+          ctxData: result?.ctxData,
         });
       }
 
@@ -268,6 +269,13 @@ class WorkflowWorker {
       }
     } catch (error) {
       console.error(error);
+
+      const errorLogData = {
+        message: error.message,
+        ...(error.data || {}),
+        ...(error.ctxData || {}),
+      };
+
       const { onError: blockOnError } = replacedBlock.data;
       if (blockOnError && blockOnError.enable) {
         if (blockOnError.retry && blockOnError.retryTimes) {
@@ -298,10 +306,7 @@ class WorkflowWorker {
           blockOnError.toDo === 'continue' ? 1 : 'fallback'
         );
         if (blockOnError.toDo !== 'error' && nextBlocks) {
-          addBlockLog('error', {
-            message: error.message,
-            ...(error.data || {}),
-          });
+          addBlockLog('error', errorLogData);
 
           this.executeNextBlocks(nextBlocks, prevBlockData);
 
@@ -309,7 +314,7 @@ class WorkflowWorker {
         }
       }
 
-      const errorLogItem = { message: error.message, ...(error.data || {}) };
+      const errorLogItem = errorLogData;
       addBlockLog('error', errorLogItem);
 
       errorLogItem.blockId = block.id;

+ 15 - 1
src/workflowEngine/blocksHandler/handlerWebhook.js

@@ -37,14 +37,28 @@ export async function webhook({ data, id }, { refData }) {
     const response = await executeWebhook({ ...data, headers: newHeaders });
 
     if (!response.ok) {
+      const { status, statusText } = response;
+      const responseData = await (data.responseType === 'json'
+        ? response.json()
+        : response.text());
+      const ctxData = {
+        ctxData: {
+          request: { status, statusText, data: responseData },
+        },
+      };
+
       if (fallbackOutput && fallbackOutput.length > 0) {
         return {
+          ctxData,
           data: '',
           nextBlockId: fallbackOutput,
         };
       }
 
-      throw new Error(`(${response.status}) ${response.statusText}`);
+      const error = new Error(`(${response.status}) ${response.statusText}`);
+      error.ctxData = ctxData;
+
+      throw error;
     }
 
     if (!data.assignVariable && !data.saveData) {