@@ -22,6 +22,7 @@ import {
22
22
RunInContext ,
23
23
withContext ,
24
24
} from "./context.js" ;
25
+ import { WorkflowExecutionContext } from "./workflow-context.js" ;
25
26
import { WorkflowMessageListener } from "./workflow-state.js" ;
26
27
27
28
export { STREAMING_PLACEHOLDER } ;
@@ -220,36 +221,16 @@ export function Component<P extends object = {}, R = unknown>(
220
221
221
222
if ( result instanceof Promise ) {
222
223
return result
223
- . then ( ( value ) => {
224
- return handleResultValue ( value , runInContext ) ;
225
- } )
224
+ . then ( ( value ) => handleResultValue ( value , runInContext ) )
226
225
. catch ( ( error : unknown ) => {
227
- if ( error instanceof Error ) {
228
- checkpointManager . addMetadata ( nodeId , {
229
- error : serializeError ( error ) ,
230
- } ) ;
231
- checkpointManager . completeNode ( nodeId , undefined ) ;
232
- workflowContext . sendWorkflowMessage ( {
233
- type : "error" ,
234
- error : JSON . stringify ( serializeError ( error ) ) ,
235
- } ) ;
236
- }
226
+ handleError ( nodeId , error , workflowContext ) ;
237
227
throw error ;
238
228
} ) as R ;
239
229
}
240
230
241
231
return handleResultValue ( result , runInContext ! ) as R ;
242
232
} catch ( error ) {
243
- if ( error instanceof Error ) {
244
- checkpointManager . addMetadata ( nodeId , {
245
- error : serializeError ( error ) ,
246
- } ) ;
247
- checkpointManager . completeNode ( nodeId , undefined ) ;
248
- workflowContext . sendWorkflowMessage ( {
249
- type : "error" ,
250
- error : JSON . stringify ( serializeError ( error ) ) ,
251
- } ) ;
252
- }
233
+ handleError ( nodeId , error , workflowContext ) ;
253
234
throw error ;
254
235
}
255
236
} ;
@@ -265,6 +246,27 @@ export function Component<P extends object = {}, R = unknown>(
265
246
return ComponentFn ;
266
247
}
267
248
249
+ function handleError (
250
+ nodeId : string ,
251
+ error : unknown ,
252
+ workflowContext : WorkflowExecutionContext ,
253
+ ) {
254
+ const serializedError = serializeError ( error ) ;
255
+ workflowContext . checkpointManager . addMetadata ( nodeId , {
256
+ error : serializedError ,
257
+ } ) ;
258
+ workflowContext . checkpointManager . completeNode ( nodeId , undefined ) ;
259
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
260
+ if ( ! ( error as any ) . __gensxErrorEventEmitted ) {
261
+ workflowContext . sendWorkflowMessage ( {
262
+ type : "error" ,
263
+ error : JSON . stringify ( serializedError ) ,
264
+ } ) ;
265
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
266
+ ( error as any ) . __gensxErrorEventEmitted = true ;
267
+ }
268
+ }
269
+
268
270
type WorkflowRuntimeOpts = WorkflowOpts & {
269
271
workflowExecutionId ?: string ;
270
272
messageListener ?: WorkflowMessageListener ;
0 commit comments