11import { createLogger } from '@sim/logger'
2- import { toError } from '@sim/utils/errors'
2+ import { describeError , toError } from '@sim/utils/errors'
33import { isRecordLike , omit } from '@sim/utils/object'
44import { isLargeValueRef } from '@/lib/execution/payloads/large-value-ref'
55import { materializeLargeValueRef , storeLargeValue } from '@/lib/execution/payloads/store'
@@ -231,17 +231,24 @@ export function copyTraceSpansWithoutCosts(spans?: TraceSpan[]): TraceSpan[] | u
231231 *
232232 * On any failure (no scope, oversized, storage error) the original (already
233233 * cost-stripped) execution data is returned unchanged so the log is never lost.
234+ * Backfills pass `throwOnError` to stop instead of retaining inline data.
234235 */
235236export async function externalizeExecutionData (
236237 executionData : Record < string , unknown > ,
237- context : TraceStoreWriteContext
238+ context : TraceStoreWriteContext ,
239+ options : { throwOnError ?: boolean } = { }
238240) : Promise < Record < string , unknown > > {
239241 const { workspaceId, workflowId, executionId, userId } = context
240242 // workspaceId/workflowId build the storage key and can be null for
241243 // deleted-workflow rows. userId is type-guaranteed by TraceStoreWriteContext;
242244 // the falsy check is a defensive guard against an empty string. If any are
243245 // missing the durable write can't succeed, so keep the data inline.
244- if ( ! workspaceId || ! workflowId || ! userId ) return executionData
246+ if ( ! workspaceId || ! workflowId || ! userId ) {
247+ if ( options . throwOnError ) {
248+ throw new Error ( 'Trace storage requires workspaceId, workflowId, and userId' )
249+ }
250+ return executionData
251+ }
245252
246253 try {
247254 const json = JSON . stringify ( executionData )
@@ -266,9 +273,10 @@ export async function externalizeExecutionData(
266273 }
267274 return slim
268275 } catch ( error ) {
276+ if ( options . throwOnError ) throw error
269277 logger . warn ( 'Failed to externalize execution data; keeping inline' , {
270278 executionId,
271- error : toError ( error ) . message ,
279+ error : describeError ( error ) ,
272280 } )
273281 return executionData
274282 }
0 commit comments