refactor: cleanup task function ops validation
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 24 Sep 2024 17:09:10 +0000 (19:09 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 24 Sep 2024 17:09:10 +0000 (19:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/worker/abstract-worker.ts

index 53ec62f9b8b0cd91e32e39987f7a20782abca5da..04515412defa7f55e302e0c4d07ca424b4776130 100644 (file)
@@ -264,6 +264,11 @@ export abstract class AbstractWorker<
   ): void {
     const { taskFunction, taskFunctionOperation, taskFunctionProperties } =
       message
+    if (typeof taskFunction !== 'string') {
+      throw new Error(
+        'Cannot handle task function operation message without task function'
+      )
+    }
     if (taskFunctionProperties == null) {
       throw new Error(
         'Cannot handle task function operation message without task function properties'
@@ -272,25 +277,18 @@ export abstract class AbstractWorker<
     let response: TaskFunctionOperationResult
     switch (taskFunctionOperation) {
       case 'add':
-        if (typeof taskFunction === 'string') {
-          response = this.addTaskFunction(taskFunctionProperties.name, {
-            // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-unsafe-call
-            taskFunction: new Function(
-              `return ${taskFunction}`
-            )() as TaskFunction<Data, Response>,
-            ...(taskFunctionProperties.priority != null && {
-              priority: taskFunctionProperties.priority,
-            }),
-            ...(taskFunctionProperties.strategy != null && {
-              strategy: taskFunctionProperties.strategy,
-            }),
-          })
-        } else {
-          response = {
-            error: new Error("'taskFunction' property is not a string"),
-            status: false,
-          }
-        }
+        response = this.addTaskFunction(taskFunctionProperties.name, {
+          // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-unsafe-call
+          taskFunction: new Function(
+            `return ${taskFunction}`
+          )() as TaskFunction<Data, Response>,
+          ...(taskFunctionProperties.priority != null && {
+            priority: taskFunctionProperties.priority,
+          }),
+          ...(taskFunctionProperties.strategy != null && {
+            strategy: taskFunctionProperties.strategy,
+          }),
+        })
         break
       case 'default':
         response = this.setDefaultTaskFunction(taskFunctionProperties.name)