chore: scope errors on workflow to invoke (#4709)

what:

Scopes the errors from the exported workflow to return only invoke errors. 

If an error occurs on the invoke level, it'll stop the invoke flow and begin the compensation flow. If another error shows up on the compensate level, it doesn't make sense to throw that error since the user won't be able to do anything about it.
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2023-08-07 17:02:38 +00:00
1 parent 8ae31aff4b
commit a42c41e8ab
2 files changed
+12 -6

No files matched your search

@@ -1,3 +1,4 @@
import { isDefined } from "@medusajs/utils"
import { TransactionFlow } from "./transaction-orchestrator" import { TransactionFlow } from "./transaction-orchestrator"
import { TransactionHandlerType, TransactionState } from "./types" import { TransactionHandlerType, TransactionState } from "./types"
@@ -108,8 +109,12 @@ export class DistributedTransaction {
return this.context return this.context
} }
public getErrors() { public getErrors(handlerType?: TransactionHandlerType) {
return this.errors if (!isDefined(handlerType)) {
return this.errors
}
return this.errors.filter((error) => error.handlerType === handlerType)
} }
public addError( public addError(
@@ -1,15 +1,16 @@
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
import { import {
DistributedTransaction, DistributedTransaction,
LocalWorkflow, LocalWorkflow,
TransactionHandlerType,
TransactionState, TransactionState,
TransactionStepError, TransactionStepError,
} from "@medusajs/orchestration" } from "@medusajs/orchestration"
import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"
import { EOL } from "os"
import { MedusaModule } from "@medusajs/modules-sdk" import { MedusaModule } from "@medusajs/modules-sdk"
import { Workflows } from "../definitions" import { EOL } from "os"
import { ulid } from "ulid" import { ulid } from "ulid"
import { Workflows } from "../definitions"
export type FlowRunOptions<TData = unknown> = { export type FlowRunOptions<TData = unknown> = {
input?: TData input?: TData
@@ -65,7 +66,7 @@ export const exportWorkflow = <TData = unknown, TResult = unknown>(
context context
) )
const errors = transaction.getErrors() const errors = transaction.getErrors(TransactionHandlerType.INVOKE)
const failedStatus = [TransactionState.FAILED, TransactionState.REVERTED] const failedStatus = [TransactionState.FAILED, TransactionState.REVERTED]
if (failedStatus.includes(transaction.getState()) && throwOnError) { if (failedStatus.includes(transaction.getState()) && throwOnError) {