Browse Source

feat(background): add error messages

Ahmad Kholid 3 years ago
parent
commit
b8937f2522

+ 16 - 13
src/background/blocks-handler.js

@@ -1,8 +1,10 @@
 /* eslint-disable no-underscore-dangle */
 /* eslint-disable no-underscore-dangle */
 import browser from 'webextension-polyfill';
 import browser from 'webextension-polyfill';
 import { objectHasKey } from '@/utils/helper';
 import { objectHasKey } from '@/utils/helper';
+import { tasks } from '@/utils/shared';
 import dataExporter from '@/utils/data-exporter';
 import dataExporter from '@/utils/data-exporter';
 import compareBlockValue from '@/utils/compare-block-value';
 import compareBlockValue from '@/utils/compare-block-value';
+import errorMessage from './error-message';
 
 
 function getBlockConnection(block, index = 1) {
 function getBlockConnection(block, index = 1) {
   const blockId = block.outputs[`output_${index}`]?.connections[0]?.node;
   const blockId = block.outputs[`output_${index}`]?.connections[0]?.node;
@@ -24,6 +26,13 @@ function convertData(data, type) {
 
 
   return result;
   return result;
 }
 }
+function generateBlockError(block) {
+  const message = errorMessage('no-tab', tasks[block.name]);
+  const error = new Error(message);
+  error.nextBlockId = getBlockConnection(block);
+
+  return error;
+}
 
 
 export function trigger(block) {
 export function trigger(block) {
   return new Promise((resolve) => {
   return new Promise((resolve) => {
@@ -38,9 +47,7 @@ export function goBack(block) {
     const nextBlockId = getBlockConnection(block);
     const nextBlockId = getBlockConnection(block);
 
 
     if (!this.tabId) {
     if (!this.tabId) {
-      const error = new Error("Can't connect to a tab");
-      error.nextBlockId = nextBlockId;
-      reject(error);
+      reject(generateBlockError(block));
 
 
       return;
       return;
     }
     }
@@ -65,9 +72,7 @@ export function forwardPage(block) {
     const nextBlockId = getBlockConnection(block);
     const nextBlockId = getBlockConnection(block);
 
 
     if (!this.tabId) {
     if (!this.tabId) {
-      const error = new Error("Can't connect to a tab");
-      error.nextBlockId = nextBlockId;
-      reject(error);
+      reject(generateBlockError(block));
 
 
       return;
       return;
     }
     }
@@ -157,10 +162,10 @@ export async function activeTab(block) {
 
 
 export function interactionHandler(block) {
 export function interactionHandler(block) {
   return new Promise((resolve, reject) => {
   return new Promise((resolve, reject) => {
+    const nextBlockId = getBlockConnection(block);
+
     if (!this.connectedTab) {
     if (!this.connectedTab) {
-      const error = new Error("Can't connect to a tab");
-      error.nextBlockId = getBlockConnection(block);
-      reject(error);
+      reject(generateBlockError(block));
 
 
       return;
       return;
     }
     }
@@ -194,7 +199,7 @@ export function interactionHandler(block) {
 
 
         resolve({
         resolve({
           data,
           data,
-          nextBlockId: getBlockConnection(block),
+          nextBlockId,
         });
         });
       },
       },
     });
     });
@@ -226,9 +231,7 @@ export function exportData(block) {
 export function elementExists(block) {
 export function elementExists(block) {
   return new Promise((resolve, reject) => {
   return new Promise((resolve, reject) => {
     if (!this.connectedTab) {
     if (!this.connectedTab) {
-      const error = new Error("Can't connect to a tab");
-      error.nextBlockId = getBlockConnection(block);
-      reject(error);
+      reject(generateBlockError(block));
 
 
       return;
       return;
     }
     }

+ 23 - 0
src/background/error-message.js

@@ -0,0 +1,23 @@
+import { objectHasKey } from '@/utils/helper';
+
+const messages = {
+  'no-trigger-block': '"{{name}}"" workflow doesn\'t have a trigger block.',
+  'no-block': '"{{name}}" workflow doesn\'t have any blocks.',
+  'no-tab':
+    'Can\'t connect to a tab, use "New tab" or "Active tab" block before using "{{name}}" block.',
+};
+
+export default function (errorId, data) {
+  const message = messages[errorId];
+
+  if (!message) return `Can't find message for this error (${errorId})`;
+
+  /* eslint-disable-next-line */
+  const resultMessage = message.replace(/{{\s*[\w\.]+\s*}}/g, (match) => {
+    const key = match.replace(/{|}/g, '');
+
+    return objectHasKey(data, key) ? data[key] : key;
+  });
+
+  return resultMessage;
+}

+ 15 - 3
src/background/workflow-engine.js

@@ -3,8 +3,9 @@ import browser from 'webextension-polyfill';
 import { nanoid } from 'nanoid';
 import { nanoid } from 'nanoid';
 import { toCamelCase } from '@/utils/helper';
 import { toCamelCase } from '@/utils/helper';
 import { tasks } from '@/utils/shared';
 import { tasks } from '@/utils/shared';
-import * as blocksHandler from './blocks-handler';
+import errorMessage from './error-message';
 import workflowState from './workflow-state';
 import workflowState from './workflow-state';
+import * as blocksHandler from './blocks-handler';
 
 
 let reloadTimeout;
 let reloadTimeout;
 
 
@@ -81,6 +82,7 @@ class WorkflowEngine {
     this.isPaused = false;
     this.isPaused = false;
     this.isDestroyed = false;
     this.isDestroyed = false;
     this.currentBlock = null;
     this.currentBlock = null;
+    this.workflowTimeout = null;
 
 
     this.tabMessageListeners = {};
     this.tabMessageListeners = {};
     this.tabUpdatedListeners = {};
     this.tabUpdatedListeners = {};
@@ -97,7 +99,7 @@ class WorkflowEngine {
     const blocks = drawflowData?.drawflow.Home.data;
     const blocks = drawflowData?.drawflow.Home.data;
 
 
     if (!blocks) {
     if (!blocks) {
-      console.error('No block is found');
+      console.error(errorMessage('no-block', this.workflow));
       return;
       return;
     }
     }
 
 
@@ -105,7 +107,7 @@ class WorkflowEngine {
     const triggerBlock = blocksArr.find(({ name }) => name === 'trigger');
     const triggerBlock = blocksArr.find(({ name }) => name === 'trigger');
 
 
     if (!triggerBlock) {
     if (!triggerBlock) {
-      console.error('A trigger block is required');
+      console.error(errorMessage('no-trigger-block', this.workflow));
       return;
       return;
     }
     }
 
 
@@ -192,6 +194,11 @@ class WorkflowEngine {
 
 
       return;
       return;
     }
     }
+    console.log(this.workflow);
+    this.workflowTimeout = setTimeout(
+      () => this.stop(),
+      this.workflow.settings.timeout || 120000
+    );
 
 
     workflowState.update(this.id, this.state);
     workflowState.update(this.id, this.state);
     console.log(`${block.name}:`, block);
     console.log(`${block.name}:`, block);
@@ -215,6 +222,9 @@ class WorkflowEngine {
             this.stop();
             this.stop();
             console.log('Done', this);
             console.log('Done', this);
           }
           }
+
+          clearTimeout(this.workflowTimeout);
+          this.workflowTimeout = null;
         })
         })
         .catch((error) => {
         .catch((error) => {
           if (
           if (
@@ -229,6 +239,8 @@ class WorkflowEngine {
             this.stop();
             this.stop();
           }
           }
 
 
+          clearTimeout(this.workflowTimeout);
+          this.workflowTimeout = null;
           console.dir(error);
           console.dir(error);
           console.error(error, 'new');
           console.error(error, 'new');
         });
         });

+ 0 - 1
src/background/workflow-state.js

@@ -44,7 +44,6 @@ class WorkflowState {
     return updater.call(
     return updater.call(
       this,
       this,
       (items, index) => {
       (items, index) => {
-        console.log(items, index, items[index]);
         if (typeof index === 'number' && index !== -1) {
         if (typeof index === 'number' && index !== -1) {
           items[index].state = { ...items[index].state, ...data };
           items[index].state = { ...items[index].state, ...data };
         }
         }

+ 2 - 1
src/models/workflow.js

@@ -18,7 +18,8 @@ class Workflow extends Model {
       lastRunAt: this.number(),
       lastRunAt: this.number(),
       createdAt: this.number(),
       createdAt: this.number(),
       settings: this.attr({
       settings: this.attr({
-        onError: 'keep-running',
+        timeout: 120000,
+        onError: 'stop-workflow',
       }),
       }),
       tasks: this.hasMany(Task, 'workflowId'),
       tasks: this.hasMany(Task, 'workflowId'),
     };
     };