fix: ensure task error proper throw with worker-threads
[poolifier.git] / src / worker / abstract-worker.ts
index 0646a9bfa0e423bd466ccc16efeba8b4782067c7..f291c52428fee35bd384a845945d64de8ee539e3 100644 (file)
@@ -206,10 +206,10 @@ export abstract class AbstractWorker<
    * Handles an error and convert it to a string so it can be sent back to the main worker.
    *
    * @param e - The error raised by the worker.
-   * @returns Message of the error.
+   * @returns The error message.
    */
   protected handleError (e: Error | string): string {
-    return e as string
+    return e instanceof Error ? e.message : e
   }
 
   /**
@@ -233,13 +233,13 @@ export abstract class AbstractWorker<
         id: message.id
       })
     } catch (e) {
-      const err = this.handleError(e as Error)
+      const errorMessage = this.handleError(e as Error | string)
       this.sendToMainWorker({
         taskError: {
-          message: err,
+          workerId: this.id,
+          message: errorMessage,
           data: message.data
         },
-        workerId: this.id,
         id: message.id
       })
     } finally {
@@ -270,13 +270,13 @@ export abstract class AbstractWorker<
         return null
       })
       .catch(e => {
-        const err = this.handleError(e as Error)
+        const errorMessage = this.handleError(e as Error | string)
         this.sendToMainWorker({
           taskError: {
-            message: err,
+            workerId: this.id,
+            message: errorMessage,
             data: message.data
           },
-          workerId: this.id,
           id: message.id
         })
       })