Skip to content

Commit ae81d04

Browse files
log(db): Add db failure cause log message
1 parent 7218185 commit ae81d04

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

apps/sim/lib/workflows/executor/execution-core.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { createLogger } from '@sim/logger'
77
import { getErrorMessage } from '@sim/utils/errors'
8+
import { filterUndefined } from '@sim/utils/object'
89
import { mergeSubblockStateWithValues } from '@sim/workflow-persistence/subblocks'
910
import type { Edge } from 'reactflow'
1011
import { z } from 'zod'
@@ -39,6 +40,45 @@ const logger = createLogger('ExecutionCore')
3940

4041
const EnvVarsSchema = z.record(z.string(), z.string())
4142

43+
/**
44+
* Surfaces the underlying driver error from a wrapped error chain.
45+
*
46+
* Drizzle wraps the original `postgres`/Node driver error as `error.cause`,
47+
* which the logger's Error serializer drops (it only emits own-enumerable
48+
* keys). Walking the `cause` chain and preferring the first error carrying a
49+
* `code` exposes the diagnostic fields — notably the Postgres `code` — that
50+
* distinguish a connection drop (`08006`), a rejected connection (`53300`),
51+
* and a statement timeout (`57014`) behind an opaque "Failed query" message.
52+
*/
53+
function describeErrorCause(error: unknown): Record<string, unknown> | undefined {
54+
try {
55+
let current: unknown = error instanceof Error ? error.cause : undefined
56+
let driver: (Error & Record<string, unknown>) | undefined
57+
for (let depth = 0; depth < 10 && current instanceof Error; depth++) {
58+
const candidate = current as Error & Record<string, unknown>
59+
if (!driver) driver = candidate
60+
if (candidate.code !== undefined) {
61+
driver = candidate
62+
break
63+
}
64+
current = candidate.cause
65+
}
66+
if (!driver) return undefined
67+
return filterUndefined({
68+
name: driver.name,
69+
message: driver.message,
70+
code: driver.code,
71+
severity: driver.severity,
72+
detail: driver.detail,
73+
routine: driver.routine,
74+
errno: driver.errno,
75+
syscall: driver.syscall,
76+
})
77+
} catch {
78+
return undefined
79+
}
80+
}
81+
4282
export interface ExecuteWorkflowCoreOptions {
4383
snapshot: ExecutionSnapshot
4484
callbacks: ExecutionCallbacks
@@ -682,7 +722,7 @@ export async function executeWorkflowCore(
682722

683723
return result
684724
} catch (error: unknown) {
685-
logger.error(`[${requestId}] Execution failed:`, error)
725+
logger.error(`[${requestId}] Execution failed:`, error, { cause: describeErrorCause(error) })
686726

687727
await waitForLifecycleCallbacks()
688728

0 commit comments

Comments
 (0)