Skip to content

Commit b7f2578

Browse files
author
priyanshu.solanki
committed
fixed the refrencing errors and making sure it propogates to the console
1 parent 3a9e5f3 commit b7f2578

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

apps/sim/executor/orchestrators/loop.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
buildSentinelEndId,
1515
buildSentinelStartId,
1616
extractBaseBlockId,
17-
hasValidInput,
1817
resolveArrayInput,
1918
validateMaxCount,
2019
} from '@/executor/utils/subflow-utils'
@@ -94,11 +93,11 @@ export class LoopOrchestrator {
9493

9594
case 'forEach': {
9695
scope.loopType = 'forEach'
97-
const items = this.resolveForEachItems(ctx, loopConfig.forEachItems)
98-
99-
if (hasValidInput(loopConfig.forEachItems) && items.length === 0) {
100-
const errorMessage =
101-
'ForEach loop collection is not a valid array. Loop execution blocked.'
96+
let items: any[]
97+
try {
98+
items = this.resolveForEachItems(ctx, loopConfig.forEachItems)
99+
} catch (error) {
100+
const errorMessage = `ForEach loop resolution failed: ${error instanceof Error ? error.message : String(error)}`
102101
logger.error(errorMessage, { loopId, forEachItems: loopConfig.forEachItems })
103102
this.addLoopErrorLog(ctx, loopId, loopType, errorMessage, {
104103
forEachItems: loopConfig.forEachItems,

apps/sim/executor/orchestrators/parallel.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
calculateBranchCount,
1212
extractBaseBlockId,
1313
extractBranchIndex,
14-
hasValidInput,
1514
parseDistributionItems,
1615
resolveArrayInput,
1716
validateMaxCount,
@@ -60,19 +59,15 @@ export class ParallelOrchestrator {
6059
): ParallelScope {
6160
const parallelConfig = this.dag.parallelConfigs.get(parallelId)
6261

63-
const items = parallelConfig ? this.resolveDistributionItems(ctx, parallelConfig) : undefined
64-
65-
if (parallelConfig?.distribution !== undefined && parallelConfig?.distribution !== null) {
66-
const rawDistribution = parallelConfig.distribution
67-
const inputWasEmptyArray = Array.isArray(rawDistribution) && rawDistribution.length === 0
68-
69-
if (hasValidInput(rawDistribution) && !inputWasEmptyArray && (!items || items.length === 0)) {
70-
const errorMessage =
71-
'Parallel distribution is not a valid array. Parallel execution blocked.'
62+
let items: any[] | undefined
63+
if (parallelConfig) {
64+
try {
65+
items = this.resolveDistributionItems(ctx, parallelConfig)
66+
} catch (error) {
67+
const errorMessage = `Parallel distribution resolution failed: ${error instanceof Error ? error.message : String(error)}`
7268
logger.error(errorMessage, {
7369
parallelId,
7470
distribution: parallelConfig.distribution,
75-
resolvedItems: items,
7671
})
7772
this.addParallelErrorLog(ctx, parallelId, errorMessage, {
7873
distribution: parallelConfig.distribution,

apps/sim/executor/utils/subflow-utils.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,6 @@ export function normalizeNodeId(nodeId: string): string {
136136
return nodeId
137137
}
138138

139-
/**
140-
* Checks if a value represents valid input (not empty/null/undefined).
141-
*/
142-
export function hasValidInput(value: any): boolean {
143-
if (value === undefined || value === null) return false
144-
if (typeof value === 'string') return value !== ''
145-
if (Array.isArray(value)) return value.length > 0
146-
if (typeof value === 'object') return Object.keys(value).length > 0
147-
return true
148-
}
149-
150139
/**
151140
* Validates that a count doesn't exceed a maximum limit.
152141
* Returns an error message if validation fails, undefined otherwise.
@@ -161,6 +150,7 @@ export function validateMaxCount(count: number, max: number, itemType: string):
161150
/**
162151
* Resolves array input at runtime. Handles arrays, objects, references, and JSON strings.
163152
* Used by both loop forEach and parallel distribution resolution.
153+
* Throws an error if resolution fails.
164154
*/
165155
export function resolveArrayInput(
166156
ctx: ExecutionContext,
@@ -177,14 +167,23 @@ export function resolveArrayInput(
177167

178168
if (typeof items === 'string') {
179169
if (items.startsWith(REFERENCE.START) && items.endsWith(REFERENCE.END) && resolver) {
180-
const resolved = resolver.resolveSingleReference(ctx, '', items)
181-
if (Array.isArray(resolved)) {
182-
return resolved
170+
try {
171+
const resolved = resolver.resolveSingleReference(ctx, '', items)
172+
if (Array.isArray(resolved)) {
173+
return resolved
174+
}
175+
if (typeof resolved === 'object' && resolved !== null) {
176+
return Object.entries(resolved)
177+
}
178+
throw new Error(`Reference "${items}" did not resolve to an array or object`)
179+
} catch (error) {
180+
if (error instanceof Error && error.message.startsWith('Reference "')) {
181+
throw error
182+
}
183+
throw new Error(
184+
`Failed to resolve reference "${items}": ${error instanceof Error ? error.message : String(error)}`
185+
)
183186
}
184-
if (typeof resolved === 'object' && resolved !== null) {
185-
return Object.entries(resolved)
186-
}
187-
return []
188187
}
189188

190189
try {
@@ -196,9 +195,12 @@ export function resolveArrayInput(
196195
if (typeof parsed === 'object' && parsed !== null) {
197196
return Object.entries(parsed)
198197
}
199-
return []
200-
} catch {
201-
return []
198+
throw new Error(`Parsed value is not an array or object`)
199+
} catch (error) {
200+
if (error instanceof Error && error.message.startsWith('Parsed value')) {
201+
throw error
202+
}
203+
throw new Error(`Failed to parse items as JSON: "${items}"`)
202204
}
203205
}
204206

@@ -208,9 +210,14 @@ export function resolveArrayInput(
208210
if (Array.isArray(resolved)) {
209211
return resolved
210212
}
211-
return []
212-
} catch {
213-
return []
213+
throw new Error(`Resolved items is not an array`)
214+
} catch (error) {
215+
if (error instanceof Error && error.message.startsWith('Resolved items')) {
216+
throw error
217+
}
218+
throw new Error(
219+
`Failed to resolve items: ${error instanceof Error ? error.message : String(error)}`
220+
)
214221
}
215222
}
216223

0 commit comments

Comments
 (0)