@@ -14,7 +14,9 @@ import {
1414 buildSentinelEndId ,
1515 buildSentinelStartId ,
1616 extractBaseBlockId ,
17+ hasValidInput ,
1718 resolveArrayInput ,
19+ validateMaxCount ,
1820} from '@/executor/utils/subflow-utils'
1921import type { VariableResolver } from '@/executor/variables/resolver'
2022import type { SerializedLoop } from '@/serializer/types'
@@ -68,17 +70,21 @@ export class LoopOrchestrator {
6870 scope . loopType = 'for'
6971 const requestedIterations = loopConfig . iterations || DEFAULTS . MAX_LOOP_ITERATIONS
7072
71- if ( requestedIterations > DEFAULTS . MAX_LOOP_ITERATIONS ) {
72- const errorMessage = `For loop iterations (${ requestedIterations } ) exceeds maximum allowed (${ DEFAULTS . MAX_LOOP_ITERATIONS } ). Loop execution blocked.`
73- logger . error ( errorMessage , { loopId, requestedIterations } )
74- this . addLoopErrorLog ( ctx , loopId , loopType , errorMessage , {
73+ const iterationError = validateMaxCount (
74+ requestedIterations ,
75+ DEFAULTS . MAX_LOOP_ITERATIONS ,
76+ 'For loop iterations'
77+ )
78+ if ( iterationError ) {
79+ logger . error ( iterationError , { loopId, requestedIterations } )
80+ this . addLoopErrorLog ( ctx , loopId , loopType , iterationError , {
7581 iterations : requestedIterations ,
7682 } )
7783 scope . maxIterations = 0
78- scope . validationError = errorMessage
84+ scope . validationError = iterationError
7985 scope . condition = buildLoopIndexCondition ( 0 )
8086 ctx . loopExecutions ?. set ( loopId , scope )
81- throw new Error ( errorMessage )
87+ throw new Error ( iterationError )
8288 }
8389
8490 scope . maxIterations = requestedIterations
@@ -89,11 +95,8 @@ export class LoopOrchestrator {
8995 case 'forEach' : {
9096 scope . loopType = 'forEach'
9197 const items = this . resolveForEachItems ( ctx , loopConfig . forEachItems )
92- const hasInput =
93- loopConfig . forEachItems !== undefined &&
94- loopConfig . forEachItems !== null &&
95- loopConfig . forEachItems !== ''
96- if ( hasInput && items . length === 0 ) {
98+
99+ if ( hasValidInput ( loopConfig . forEachItems ) && items . length === 0 ) {
97100 const errorMessage =
98101 'ForEach loop collection is not a valid array. Loop execution blocked.'
99102 logger . error ( errorMessage , { loopId, forEachItems : loopConfig . forEachItems } )
@@ -108,20 +111,23 @@ export class LoopOrchestrator {
108111 throw new Error ( errorMessage )
109112 }
110113
111- const originalLength = items . length
112- if ( originalLength > DEFAULTS . MAX_FOREACH_ITEMS ) {
113- const errorMessage = `ForEach loop collection size (${ originalLength } ) exceeds maximum allowed (${ DEFAULTS . MAX_FOREACH_ITEMS } ). Loop execution blocked.`
114- logger . error ( errorMessage , { loopId, originalLength } )
115- this . addLoopErrorLog ( ctx , loopId , loopType , errorMessage , {
114+ const sizeError = validateMaxCount (
115+ items . length ,
116+ DEFAULTS . MAX_FOREACH_ITEMS ,
117+ 'ForEach loop collection size'
118+ )
119+ if ( sizeError ) {
120+ logger . error ( sizeError , { loopId, collectionSize : items . length } )
121+ this . addLoopErrorLog ( ctx , loopId , loopType , sizeError , {
116122 forEachItems : loopConfig . forEachItems ,
117- collectionSize : originalLength ,
123+ collectionSize : items . length ,
118124 } )
119125 scope . items = [ ]
120126 scope . maxIterations = 0
121- scope . validationError = errorMessage
127+ scope . validationError = sizeError
122128 scope . condition = buildLoopIndexCondition ( 0 )
123129 ctx . loopExecutions ?. set ( loopId , scope )
124- throw new Error ( errorMessage )
130+ throw new Error ( sizeError )
125131 }
126132
127133 scope . items = items
@@ -143,17 +149,21 @@ export class LoopOrchestrator {
143149 } else {
144150 const requestedIterations = loopConfig . iterations || DEFAULTS . MAX_LOOP_ITERATIONS
145151
146- if ( requestedIterations > DEFAULTS . MAX_LOOP_ITERATIONS ) {
147- const errorMessage = `Do-While loop iterations (${ requestedIterations } ) exceeds maximum allowed (${ DEFAULTS . MAX_LOOP_ITERATIONS } ). Loop execution blocked.`
148- logger . error ( errorMessage , { loopId, requestedIterations } )
149- this . addLoopErrorLog ( ctx , loopId , loopType , errorMessage , {
152+ const iterationError = validateMaxCount (
153+ requestedIterations ,
154+ DEFAULTS . MAX_LOOP_ITERATIONS ,
155+ 'Do-While loop iterations'
156+ )
157+ if ( iterationError ) {
158+ logger . error ( iterationError , { loopId, requestedIterations } )
159+ this . addLoopErrorLog ( ctx , loopId , loopType , iterationError , {
150160 iterations : requestedIterations ,
151161 } )
152162 scope . maxIterations = 0
153- scope . validationError = errorMessage
163+ scope . validationError = iterationError
154164 scope . condition = buildLoopIndexCondition ( 0 )
155165 ctx . loopExecutions ?. set ( loopId , scope )
156- throw new Error ( errorMessage )
166+ throw new Error ( iterationError )
157167 }
158168
159169 scope . maxIterations = requestedIterations
0 commit comments