chore(medusa, workflows-sdk): Workflow default to throw the first error (#7442)

**What**
Currently when a workflow fail it will throw an error which contains the messages of all error thrown durring the workflow lifetime. Therefore, in our cases we are always preventing workflow to throw and grab the first error that we then re throw.
This pr eliminate that need by throwing back the first error among the error thrown by a workflow as it is the main case. In case someone need a special handling they can still set the option throwOnError to false and handle the error the way they need
This commit is contained in:
Adrien de Peretti
2024-05-24 14:55:05 +02:00
committed by GitHub
parent 16651f9849
commit fff1b3ef9c
109 changed files with 147 additions and 773 deletions

View File

@@ -1 +0,0 @@
export const emptyHandler: any = () => {}

View File

@@ -1,5 +1,4 @@
export * from "./merge-data"
export * from "./empty-handler"
export * from "./pipe"
export * from "./workflow-export"
export * from "./type"

View File

@@ -4,6 +4,7 @@ import { isObject } from "@medusajs/utils"
/**
* Pipe utils that merges data from an object into a new object.
* The new object will have a target key with the merged data from the keys if specified.
* @deprecated
* @param keys
* @param target
*/

View File

@@ -66,6 +66,11 @@ export type PipelineHandler<T extends any = undefined> = (
: T
>
/**
* @deprecated
* @param input
* @param functions
*/
export function pipe<T>(
input: PipelineInput,
...functions: [...PipelineHandler[], PipelineHandler<T>]

View File

@@ -5,7 +5,7 @@ import {
TransactionState,
} from "@medusajs/orchestration"
import { LoadedModule, MedusaContainer } from "@medusajs/types"
import { MedusaContextType, isPresent } from "@medusajs/utils"
import { isPresent, MedusaContextType } from "@medusajs/utils"
import { EOL } from "os"
import { ulid } from "ulid"
import { MedusaWorkflow } from "../medusa-workflow"
@@ -87,10 +87,11 @@ function createContextualWorkflowRunner<
failedStatus.includes(transaction.getState()) &&
throwOnError
) {
const errorMessage = errors
/*const errorMessage = errors
?.map((err) => `${err.error?.message}${EOL}${err.error?.stack}`)
?.join(`${EOL}`)
throw new Error(errorMessage)
?.join(`${EOL}`)*/
const firstError = errors?.[0]?.error ?? new Error("Unknown error")
throw firstError
}
let result