Files
zpan/server/lib/errors.ts
saltbo d2f3a34f05 fix(server): log underlying cause chain for failed D1 queries
Drizzle wraps the real D1 error in DrizzleQueryError.cause; both the access
log and the origin-detect catch only logged .message, surfacing just
"Failed query: <sql>" with no reason. Add formatError() to flatten the cause
chain and use it at both sites so D1 failures are diagnosable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 00:42:50 -04:00

21 lines
718 B
TypeScript

/**
* Flatten an error and its `cause` chain into a single string.
*
* Drizzle wraps the underlying driver error in `DrizzleQueryError`, whose
* `message` is only `Failed query: <sql>`; the real D1 error lives in `.cause`.
* Logging just `.message` hides why the query failed — this surfaces the chain.
*/
export function formatError(error: unknown): string {
if (!(error instanceof Error)) return String(error)
const parts: string[] = [error.message]
let cause = (error as { cause?: unknown }).cause
while (cause instanceof Error) {
parts.push(cause.message)
cause = (cause as { cause?: unknown }).cause
}
if (cause !== undefined) parts.push(String(cause))
return parts.join(' <- ')
}