Browse Source

Merge pull request #1667 from AprildreamMI/workflow-state-block-error

Added the ability to throw errors and customize error messages for the "workflow-state" task block.
Ahmad Kholid 1 year ago
parent
commit
5717629a8e

+ 19 - 0
src/components/newtab/workflow/edit/EditWorkflowState.vue

@@ -30,6 +30,25 @@
     >
       Execpt for the current workflow
     </ui-checkbox>
+    <!-- 停止当前工作流 是否抛出错误及自定义错误信息 -->
+    <template v-if="data.type === 'stop-current'">
+      <ui-checkbox
+        :model-value="data.throwError"
+        block
+        class="block-variable mt-4"
+        @change="updateData({ throwError: $event })"
+      >
+        {{ t(`workflow.blocks.workflow-state.error.throwError`) }}
+      </ui-checkbox>
+      <ui-input
+        v-if="data.throwError"
+        :model-value="data.errorMessage"
+        :placeholder="t(`workflow.blocks.workflow-state.error.message`)"
+        :title="t(`workflow.blocks.workflow-state.error.message`)"
+        class="mt-2 w-full"
+        @change="updateData({ errorMessage: $event })"
+      />
+    </template>
     <div
       v-if="data.type === 'stop-specific'"
       class="bg-input focus-within:bg-box-transparent-2 mt-4 rounded-lg transition"

+ 5 - 1
src/locales/en/blocks.json

@@ -133,6 +133,10 @@
         "description": "Manage workflows states",
         "actions": {
           "stop": "Stop workflows"
+        },
+        "error": {
+          "throwError": "Throw error",
+          "message": "Error message"
         }
       },
       "regex-variable": {
@@ -803,4 +807,4 @@
       }
     }
   }
-}
+}

+ 5 - 1
src/locales/zh/blocks.json

@@ -133,6 +133,10 @@
         "description": "管理工作流状态",
         "actions": {
           "stop": "停止工作流"
+        },
+        "error": {
+          "throwError": "Throw error",
+          "message": "Error message"
         }
       },
       "regex-variable": {
@@ -796,4 +800,4 @@
       }
     }
   }
-}
+}

+ 3 - 0
src/utils/shared.js

@@ -1446,12 +1446,15 @@ export const tasks = {
     outputs: 1,
     allowedInputs: true,
     maxConnection: 1,
+    refDataKeys: ['errorMessage'],
     data: {
       disableBlock: false,
       description: '',
       type: 'stop-current',
       exceptCurrent: false,
       workflowsToStop: [],
+      throwError: false,
+      errorMessage: '',
     },
   },
   'parameter-prompt': {

+ 36 - 24
src/workflowEngine/blocksHandler/handlerWorkflowState.js

@@ -1,33 +1,45 @@
 export default async function ({ data, id }) {
-  let stopCurrent = false;
+  try {
+    let stopCurrent = false;
 
-  if (data.type === 'stop-current') {
-    return {};
-  }
-  if (['stop-specific', 'stop-all'].includes(data.type)) {
-    const ids = [];
-    const isSpecific = data.type === 'stop-specific';
-    this.engine.states.getAll.forEach((state) => {
-      const workflowNotIncluded =
-        isSpecific && !data.workflowsToStop.includes(state.workflowId);
-      if (workflowNotIncluded) return;
+    if (data.type === 'stop-current') {
+      // 如果需要抛出错误
+      if (data.throwError) {
+        throw new Error(data.errorMessage || 'Workflow stopped manually');
+      } else {
+        return {};
+      }
+    }
+    if (['stop-specific', 'stop-all'].includes(data.type)) {
+      const ids = [];
+      const isSpecific = data.type === 'stop-specific';
+      this.engine.states.getAll.forEach((state) => {
+        const workflowNotIncluded =
+          isSpecific && !data.workflowsToStop.includes(state.workflowId);
+        if (workflowNotIncluded) return;
 
-      ids.push(state.id);
-    });
+        ids.push(state.id);
+      });
 
-    for (const stateId of ids) {
-      if (stateId === this.engine.id) {
-        stopCurrent = isSpecific ? true : !data.exceptCurrent;
-      } else {
-        await this.engine.states.stop(stateId);
+      for (const stateId of ids) {
+        if (stateId === this.engine.id) {
+          stopCurrent = isSpecific ? true : !data.exceptCurrent;
+        } else {
+          await this.engine.states.stop(stateId);
+        }
       }
     }
-  }
 
-  if (stopCurrent) return {};
+    if (stopCurrent) return {};
 
-  return {
-    data: '',
-    nextBlockId: this.getBlockConnections(id),
-  };
+    return {
+      data: '',
+      nextBlockId: this.getBlockConnections(id),
+    };
+  } catch (error) {
+    error.data = error.data || {};
+    console.error(error);
+
+    throw error;
+  }
 }