From a2beda77c4ad54948e8416bfc8c4087993123658 Mon Sep 17 00:00:00 2001 From: MythEclipse Date: Sun, 14 Jun 2026 04:40:22 +0700 Subject: [PATCH] fix(db): surface PG error code/detail/cause in voice recording insert failures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drizzle wraps PG errors — the previous log only showed the formatted query text. Now extracts code, detail, schema, table, constraint, severity from the error envelope and from .cause to make the actual Postgres error visible. Co-Authored-By: Claude --- .../src/shared/database/voiceRecordingRepo.ts | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/services/discord-gateway/src/shared/database/voiceRecordingRepo.ts b/services/discord-gateway/src/shared/database/voiceRecordingRepo.ts index 7733921..0ccead7 100644 --- a/services/discord-gateway/src/shared/database/voiceRecordingRepo.ts +++ b/services/discord-gateway/src/shared/database/voiceRecordingRepo.ts @@ -40,13 +40,35 @@ export async function insertVoiceRecording( .values(recording) .onConflictDoNothing(); } catch (error) { - const err = error as Record; + // Drizzle wraps PG errors — dig into cause + enumerable props for the real PG error + const err = error as Error & Record; const detail: Record = { - message: error instanceof Error ? error.message : String(error), - name: error instanceof Error ? error.name : undefined, + message: err.message, + name: err.name, }; - if (err.code !== undefined) detail.code = err.code; - if (err.detail !== undefined) detail.detail = err.detail; + // node-postgres native error props + for (const k of [ + "code", + "detail", + "schema", + "table", + "constraint", + "severity", + ]) { + if (err[k] !== undefined) detail[k] = err[k]; + } + // drizzle may stash the original in .cause + const cause = err.cause; + if (cause instanceof Error) { + const causeErr = cause as Error & Record; + detail.cause = { + message: causeErr.message, + name: causeErr.name, + code: causeErr.code, + detail: causeErr.detail, + constraint: causeErr.constraint, + }; + } logger.error( { id: recording.id, error: detail }, "Failed to insert voice recording",