Skip to content

Commit 4f1bc29

Browse files
committed
address error handling at correct level
1 parent 2472e70 commit 4f1bc29

3 files changed

Lines changed: 72 additions & 83 deletions

File tree

apps/sim/app/api/workflows/route.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,7 @@ export async function POST(req: NextRequest) {
269269
variables: {},
270270
})
271271

272-
const saveResult = await saveWorkflowToNormalizedTables(workflowId, workflowState, tx)
273-
if (!saveResult.success) {
274-
throw new Error(saveResult.error || 'Failed to persist default workflow blocks')
275-
}
272+
await saveWorkflowToNormalizedTables(workflowId, workflowState, tx)
276273
})
277274

278275
logger.info(`[${requestId}] Successfully created workflow ${workflowId} with default blocks`)

apps/sim/app/api/workspaces/route.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ async function createWorkspace(
175175
})
176176

177177
const { workflowState } = buildDefaultWorkflowArtifacts()
178-
const seedResult = await saveWorkflowToNormalizedTables(workflowId, workflowState, tx)
179-
if (!seedResult.success) {
180-
throw new Error(seedResult.error || 'Failed to seed default workflow state')
181-
}
178+
await saveWorkflowToNormalizedTables(workflowId, workflowState, tx)
182179
}
183180

184181
logger.info(

apps/sim/lib/workflows/persistence/utils.ts

Lines changed: 70 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -534,91 +534,86 @@ export async function saveWorkflowToNormalizedTables(
534534
state: WorkflowState,
535535
externalTx?: DbOrTx
536536
): Promise<{ success: boolean; error?: string }> {
537-
try {
538-
const blockRecords = state.blocks as Record<string, BlockState>
539-
const canonicalLoops = generateLoopBlocks(blockRecords)
540-
const canonicalParallels = generateParallelBlocks(blockRecords)
541-
542-
const execute = async (tx: DbOrTx) => {
543-
await Promise.all([
544-
tx.delete(workflowBlocks).where(eq(workflowBlocks.workflowId, workflowId)),
545-
tx.delete(workflowEdges).where(eq(workflowEdges.workflowId, workflowId)),
546-
tx.delete(workflowSubflows).where(eq(workflowSubflows.workflowId, workflowId)),
547-
])
548-
549-
// Insert blocks
550-
if (Object.keys(state.blocks).length > 0) {
551-
const blockInserts = Object.values(state.blocks).map((block) => ({
552-
id: block.id,
553-
workflowId: workflowId,
554-
type: block.type,
555-
name: block.name || '',
556-
positionX: String(block.position?.x || 0),
557-
positionY: String(block.position?.y || 0),
558-
enabled: block.enabled ?? true,
559-
horizontalHandles: block.horizontalHandles ?? true,
560-
advancedMode: block.advancedMode ?? false,
561-
triggerMode: block.triggerMode ?? false,
562-
height: String(block.height || 0),
563-
subBlocks: block.subBlocks || {},
564-
outputs: block.outputs || {},
565-
data: block.data || {},
566-
parentId: block.data?.parentId || null,
567-
extent: block.data?.extent || null,
568-
locked: block.locked ?? false,
569-
}))
570-
571-
await tx.insert(workflowBlocks).values(blockInserts)
572-
}
537+
const blockRecords = state.blocks as Record<string, BlockState>
538+
const canonicalLoops = generateLoopBlocks(blockRecords)
539+
const canonicalParallels = generateParallelBlocks(blockRecords)
540+
541+
const execute = async (tx: DbOrTx) => {
542+
await Promise.all([
543+
tx.delete(workflowBlocks).where(eq(workflowBlocks.workflowId, workflowId)),
544+
tx.delete(workflowEdges).where(eq(workflowEdges.workflowId, workflowId)),
545+
tx.delete(workflowSubflows).where(eq(workflowSubflows.workflowId, workflowId)),
546+
])
573547

574-
// Insert edges
575-
if (state.edges.length > 0) {
576-
const edgeInserts = state.edges.map((edge) => ({
577-
id: edge.id,
578-
workflowId: workflowId,
579-
sourceBlockId: edge.source,
580-
targetBlockId: edge.target,
581-
sourceHandle: edge.sourceHandle || null,
582-
targetHandle: edge.targetHandle || null,
583-
}))
584-
585-
await tx.insert(workflowEdges).values(edgeInserts)
586-
}
548+
if (Object.keys(state.blocks).length > 0) {
549+
const blockInserts = Object.values(state.blocks).map((block) => ({
550+
id: block.id,
551+
workflowId: workflowId,
552+
type: block.type,
553+
name: block.name || '',
554+
positionX: String(block.position?.x || 0),
555+
positionY: String(block.position?.y || 0),
556+
enabled: block.enabled ?? true,
557+
horizontalHandles: block.horizontalHandles ?? true,
558+
advancedMode: block.advancedMode ?? false,
559+
triggerMode: block.triggerMode ?? false,
560+
height: String(block.height || 0),
561+
subBlocks: block.subBlocks || {},
562+
outputs: block.outputs || {},
563+
data: block.data || {},
564+
parentId: block.data?.parentId || null,
565+
extent: block.data?.extent || null,
566+
locked: block.locked ?? false,
567+
}))
568+
569+
await tx.insert(workflowBlocks).values(blockInserts)
570+
}
587571

588-
// Insert subflows (loops and parallels)
589-
const subflowInserts: SubflowInsert[] = []
572+
if (state.edges.length > 0) {
573+
const edgeInserts = state.edges.map((edge) => ({
574+
id: edge.id,
575+
workflowId: workflowId,
576+
sourceBlockId: edge.source,
577+
targetBlockId: edge.target,
578+
sourceHandle: edge.sourceHandle || null,
579+
targetHandle: edge.targetHandle || null,
580+
}))
581+
582+
await tx.insert(workflowEdges).values(edgeInserts)
583+
}
590584

591-
// Add loops
592-
Object.values(canonicalLoops).forEach((loop) => {
593-
subflowInserts.push({
594-
id: loop.id,
595-
workflowId: workflowId,
596-
type: SUBFLOW_TYPES.LOOP,
597-
config: loop,
598-
})
585+
const subflowInserts: SubflowInsert[] = []
586+
587+
Object.values(canonicalLoops).forEach((loop) => {
588+
subflowInserts.push({
589+
id: loop.id,
590+
workflowId: workflowId,
591+
type: SUBFLOW_TYPES.LOOP,
592+
config: loop,
599593
})
594+
})
600595

601-
// Add parallels
602-
Object.values(canonicalParallels).forEach((parallel) => {
603-
subflowInserts.push({
604-
id: parallel.id,
605-
workflowId: workflowId,
606-
type: SUBFLOW_TYPES.PARALLEL,
607-
config: parallel,
608-
})
596+
Object.values(canonicalParallels).forEach((parallel) => {
597+
subflowInserts.push({
598+
id: parallel.id,
599+
workflowId: workflowId,
600+
type: SUBFLOW_TYPES.PARALLEL,
601+
config: parallel,
609602
})
603+
})
610604

611-
if (subflowInserts.length > 0) {
612-
await tx.insert(workflowSubflows).values(subflowInserts)
613-
}
605+
if (subflowInserts.length > 0) {
606+
await tx.insert(workflowSubflows).values(subflowInserts)
614607
}
608+
}
615609

616-
if (externalTx) {
617-
await execute(externalTx)
618-
} else {
619-
await db.transaction(execute)
620-
}
610+
if (externalTx) {
611+
await execute(externalTx)
612+
return { success: true }
613+
}
621614

615+
try {
616+
await db.transaction(execute)
622617
return { success: true }
623618
} catch (error) {
624619
logger.error(`Error saving workflow ${workflowId} to normalized tables:`, error)

0 commit comments

Comments
 (0)