From a42c41e8ab78c28e1f9a92c6043914b5a1d8a216 Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Mon, 7 Aug 2023 19:02:38 +0200 Subject: [PATCH] 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. --- .../src/transaction/distributed-transaction.ts | 9 +++++++-- packages/workflows/src/helper/workflow-export.ts | 9 +++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/orchestration/src/transaction/distributed-transaction.ts b/packages/orchestration/src/transaction/distributed-transaction.ts index b4c85c1921..834a53f927 100644 --- a/packages/orchestration/src/transaction/distributed-transaction.ts +++ b/packages/orchestration/src/transaction/distributed-transaction.ts @@ -1,3 +1,4 @@ +import { isDefined } from "@medusajs/utils" import { TransactionFlow } from "./transaction-orchestrator" import { TransactionHandlerType, TransactionState } from "./types" @@ -108,8 +109,12 @@ export class DistributedTransaction { return this.context } - public getErrors() { - return this.errors + public getErrors(handlerType?: TransactionHandlerType) { + if (!isDefined(handlerType)) { + return this.errors + } + + return this.errors.filter((error) => error.handlerType === handlerType) } public addError( diff --git a/packages/workflows/src/helper/workflow-export.ts b/packages/workflows/src/helper/workflow-export.ts index b77718ba87..cd95087cdb 100644 --- a/packages/workflows/src/helper/workflow-export.ts +++ b/packages/workflows/src/helper/workflow-export.ts @@ -1,15 +1,16 @@ -import { Context, LoadedModule, MedusaContainer } from "@medusajs/types" import { DistributedTransaction, LocalWorkflow, + TransactionHandlerType, TransactionState, TransactionStepError, } from "@medusajs/orchestration" +import { Context, LoadedModule, MedusaContainer } from "@medusajs/types" -import { EOL } from "os" import { MedusaModule } from "@medusajs/modules-sdk" -import { Workflows } from "../definitions" +import { EOL } from "os" import { ulid } from "ulid" +import { Workflows } from "../definitions" export type FlowRunOptions = { input?: TData @@ -65,7 +66,7 @@ export const exportWorkflow = ( context ) - const errors = transaction.getErrors() + const errors = transaction.getErrors(TransactionHandlerType.INVOKE) const failedStatus = [TransactionState.FAILED, TransactionState.REVERTED] if (failedStatus.includes(transaction.getState()) && throwOnError) {