Skip to content

Commit 6c9e0ec

Browse files
improvement(logging): capture pre-execution validation errors in logging session (#1124)
* improvement(pre-exec-errors): capture pre-execution validation errors in logging session * fix param shape for schedules * fix naming
1 parent bbbf1c2 commit 6c9e0ec

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

apps/sim/app/api/schedules/execute/route.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,10 @@ export async function GET() {
474474
})
475475

476476
await loggingSession.safeCompleteWithError({
477-
message: `Schedule execution failed before workflow started: ${earlyError.message}`,
478-
stackTrace: earlyError.stack,
477+
error: {
478+
message: `Schedule execution failed before workflow started: ${earlyError.message}`,
479+
stackTrace: earlyError.stack,
480+
},
479481
})
480482
} catch (loggingError) {
481483
logger.error(
@@ -591,8 +593,10 @@ export async function GET() {
591593
})
592594

593595
await failureLoggingSession.safeCompleteWithError({
594-
message: `Schedule execution failed: ${error.message}`,
595-
stackTrace: error.stack,
596+
error: {
597+
message: `Schedule execution failed: ${error.message}`,
598+
stackTrace: error.stack,
599+
},
596600
})
597601
} catch (loggingError) {
598602
logger.error(

apps/sim/lib/logs/execution/logging-session.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
createTriggerObject,
88
loadWorkflowStateForExecution,
99
} from '@/lib/logs/execution/logging-factory'
10-
import type { ExecutionEnvironment, ExecutionTrigger, WorkflowState } from '@/lib/logs/types'
10+
import type {
11+
ExecutionEnvironment,
12+
ExecutionTrigger,
13+
TraceSpan,
14+
WorkflowState,
15+
} from '@/lib/logs/types'
1116

1217
const logger = createLogger('LoggingSession')
1318

@@ -25,6 +30,15 @@ export interface SessionCompleteParams {
2530
traceSpans?: any[]
2631
}
2732

33+
export interface SessionErrorCompleteParams {
34+
endedAt?: string
35+
totalDurationMs?: number
36+
error?: {
37+
message?: string
38+
stackTrace?: string
39+
}
40+
}
41+
2842
export class LoggingSession {
2943
private workflowId: string
3044
private executionId: string
@@ -115,8 +129,14 @@ export class LoggingSession {
115129
}
116130
}
117131

118-
async completeWithError(error?: any): Promise<void> {
132+
async completeWithError(params: SessionErrorCompleteParams = {}): Promise<void> {
119133
try {
134+
const { endedAt, totalDurationMs, error } = params
135+
136+
const endTime = endedAt ? new Date(endedAt) : new Date()
137+
const durationMs = typeof totalDurationMs === 'number' ? totalDurationMs : 0
138+
const startTime = new Date(endTime.getTime() - Math.max(1, durationMs))
139+
120140
const costSummary = {
121141
totalCost: BASE_EXECUTION_CHARGE,
122142
totalInputCost: 0,
@@ -129,13 +149,29 @@ export class LoggingSession {
129149
models: {},
130150
}
131151

152+
const message = error?.message || 'Execution failed before starting blocks'
153+
154+
const syntheticErrorSpan: TraceSpan[] = [
155+
{
156+
id: 'pre-execution-validation',
157+
name: 'Workflow Error',
158+
type: 'validation',
159+
duration: Math.max(1, durationMs),
160+
startTime: startTime.toISOString(),
161+
endTime: endTime.toISOString(),
162+
status: 'error',
163+
children: [],
164+
output: { error: message },
165+
},
166+
]
167+
132168
await executionLogger.completeWorkflowExecution({
133169
executionId: this.executionId,
134-
endedAt: new Date().toISOString(),
135-
totalDurationMs: 0,
170+
endedAt: endTime.toISOString(),
171+
totalDurationMs: Math.max(1, durationMs),
136172
costSummary,
137-
finalOutput: null,
138-
traceSpans: [],
173+
finalOutput: { error: message },
174+
traceSpans: syntheticErrorSpan,
139175
})
140176

141177
if (this.requestId) {
@@ -170,7 +206,7 @@ export class LoggingSession {
170206
}
171207
}
172208

173-
async safeCompleteWithError(error?: any): Promise<void> {
209+
async safeCompleteWithError(error?: SessionErrorCompleteParams): Promise<void> {
174210
try {
175211
await this.completeWithError(error)
176212
} catch (enhancedError) {

0 commit comments

Comments
 (0)