fix(workflows): compensation handling (#5691)
This commit is contained in:
committed by
GitHub
parent
6025c702f3
commit
010560fd2a
5
.changeset/small-apes-own.md
Normal file
5
.changeset/small-apes-own.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/workflows": patch
|
||||
---
|
||||
|
||||
Fix(workflows): compensation handling
|
||||
@@ -1842,4 +1842,41 @@ describe("Workflow composer", function () {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should compose a workflow that throws without crashing and the compensation will receive undefined for the step that fails", async () => {
|
||||
const mockStep1Fn = jest.fn().mockImplementation(function (input) {
|
||||
throw new Error("invoke fail")
|
||||
})
|
||||
|
||||
const mockCompensateSte1 = jest.fn().mockImplementation(function (input) {
|
||||
return input
|
||||
})
|
||||
|
||||
const step1 = createStep("step1", mockStep1Fn, mockCompensateSte1)
|
||||
|
||||
const workflow = createWorkflow("workflow1", function (input) {
|
||||
return step1(input)
|
||||
})
|
||||
|
||||
const workflowInput = { test: "payload1" }
|
||||
const { errors } = await workflow().run({
|
||||
input: workflowInput,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
expect(errors).toHaveLength(1)
|
||||
expect(errors[0]).toEqual({
|
||||
action: "step1",
|
||||
handlerType: "invoke",
|
||||
error: expect.objectContaining({
|
||||
message: "invoke fail",
|
||||
}),
|
||||
})
|
||||
|
||||
expect(mockStep1Fn).toHaveBeenCalledTimes(1)
|
||||
expect(mockCompensateSte1).toHaveBeenCalledTimes(1)
|
||||
|
||||
expect(mockStep1Fn.mock.calls[0][0]).toEqual(workflowInput)
|
||||
expect(mockCompensateSte1.mock.calls[0][0]).toEqual(undefined)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -33,7 +33,7 @@ type InvokeFn<TInput extends object, TOutput, TCompensateInput> = (
|
||||
>>
|
||||
|
||||
type CompensateFn<T> = (
|
||||
input: T,
|
||||
input: T | undefined,
|
||||
context: StepExecutionContext
|
||||
) => unknown | Promise<unknown>
|
||||
|
||||
@@ -121,7 +121,7 @@ function applyStep<
|
||||
context: transactionContext.context,
|
||||
}
|
||||
|
||||
const stepOutput = transactionContext.invoke[stepName].output
|
||||
const stepOutput = transactionContext.invoke[stepName]?.output
|
||||
const invokeResult =
|
||||
stepOutput?.__type === SymbolWorkflowStepResponse
|
||||
? stepOutput.compensateInput &&
|
||||
|
||||
Reference in New Issue
Block a user