fix(workflows): compensation handling (#5691)

This commit is contained in:
Adrien de Peretti authored and GitHub committed 2023-11-23 09:11:03 +00:00
1 parent 6025c702f3
commit 010560fd2a
3 files changed
+44 -2

No files matched your search

+5
View 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> = ( type CompensateFn<T> = (
input: T, input: T | undefined,
context: StepExecutionContext context: StepExecutionContext
) => unknown | Promise<unknown> ) => unknown | Promise<unknown>
@@ -121,7 +121,7 @@ function applyStep<
context: transactionContext.context, context: transactionContext.context,
} }
const stepOutput = transactionContext.invoke[stepName].output const stepOutput = transactionContext.invoke[stepName]?.output
const invokeResult = const invokeResult =
stepOutput?.__type === SymbolWorkflowStepResponse stepOutput?.__type === SymbolWorkflowStepResponse
? stepOutput.compensateInput && ? stepOutput.compensateInput &&