@@ -4,12 +4,14 @@ import type { TraceSpan } from '@/lib/logs/types'
44import type { BlockOutput } from '@/blocks/types'
55import { Executor } from '@/executor'
66import { BlockType , DEFAULTS , HTTP } from '@/executor/constants'
7+ import { ChildWorkflowError } from '@/executor/errors/child-workflow-error'
78import type {
89 BlockHandler ,
910 ExecutionContext ,
1011 ExecutionResult ,
1112 StreamingExecution ,
1213} from '@/executor/types'
14+ import { hasExecutionResult } from '@/executor/utils/errors'
1315import { buildAPIUrl , buildAuthHeaders } from '@/executor/utils/http'
1416import { parseJSON } from '@/executor/utils/json'
1517import { lazyCleanupInputMapping } from '@/executor/utils/lazy-cleanup'
@@ -137,39 +139,39 @@ export class WorkflowBlockHandler implements BlockHandler {
137139 )
138140
139141 return mappedResult
140- } catch ( error : any ) {
142+ } catch ( error : unknown ) {
141143 logger . error ( `Error executing child workflow ${ workflowId } :` , error )
142144
143145 const { workflows } = useWorkflowRegistry . getState ( )
144146 const workflowMetadata = workflows [ workflowId ]
145147 const childWorkflowName = workflowMetadata ?. name || workflowId
146148
147- const originalError = error . message || 'Unknown error'
148- const wrappedError = new Error (
149- `Error in child workflow "${ childWorkflowName } ": ${ originalError } `
150- )
149+ const originalError = error instanceof Error ? error . message : 'Unknown error'
150+ let childTraceSpans : WorkflowTraceSpan [ ] = [ ]
151+ let executionResult : ExecutionResult | undefined
151152
152- if ( error . executionResult ? .logs ) {
153- const executionResult = error . executionResult as ExecutionResult
153+ if ( hasExecutionResult ( error ) && error . executionResult . logs ) {
154+ executionResult = error . executionResult
154155
155156 logger . info ( `Extracting child trace spans from error.executionResult` , {
156157 hasLogs : ( executionResult . logs ?. length ?? 0 ) > 0 ,
157158 logCount : executionResult . logs ?. length ?? 0 ,
158159 } )
159160
160- const childTraceSpans = this . captureChildWorkflowLogs (
161- executionResult ,
162- childWorkflowName ,
163- ctx
164- )
161+ childTraceSpans = this . captureChildWorkflowLogs ( executionResult , childWorkflowName , ctx )
165162
166163 logger . info ( `Captured ${ childTraceSpans . length } child trace spans from failed execution` )
167- ; ( wrappedError as any ) . childTraceSpans = childTraceSpans
168- } else if ( error . childTraceSpans && Array . isArray ( error . childTraceSpans ) ) {
169- ; ( wrappedError as any ) . childTraceSpans = error . childTraceSpans
164+ } else if ( ChildWorkflowError . isChildWorkflowError ( error ) ) {
165+ childTraceSpans = error . childTraceSpans
170166 }
171167
172- throw wrappedError
168+ throw new ChildWorkflowError ( {
169+ message : `Error in child workflow "${ childWorkflowName } ": ${ originalError } ` ,
170+ childWorkflowName,
171+ childTraceSpans,
172+ executionResult,
173+ cause : error instanceof Error ? error : undefined ,
174+ } )
173175 }
174176 }
175177
@@ -441,11 +443,11 @@ export class WorkflowBlockHandler implements BlockHandler {
441443
442444 if ( ! success ) {
443445 logger . warn ( `Child workflow ${ childWorkflowName } failed` )
444- const error = new Error (
445- `Error in child workflow "${ childWorkflowName } ": ${ childResult . error || 'Child workflow execution failed' } `
446- )
447- ; ( error as any ) . childTraceSpans = childTraceSpans || [ ]
448- throw error
446+ throw new ChildWorkflowError ( {
447+ message : `Error in child workflow "${ childWorkflowName } ": ${ childResult . error || 'Child workflow execution failed' } ` ,
448+ childWorkflowName ,
449+ childTraceSpans : childTraceSpans || [ ] ,
450+ } )
449451 }
450452
451453 return {
0 commit comments