Open History to see the last commit
1/**
2 * Error text including the `cause` chain. Drizzle wraps the real D1 error
3 * (e.g. `D1_ERROR: ...`) in a DrizzleQueryError whose own message is the
4 * full SQL + params dump — without the chain the actual failure is lost.
5 * Each link is truncated so query/params dumps can't bloat logs past what
6 * Workers Logs stores.
7 */
8function describeThrown(value: unknown): string {
9 if (
10 typeof value === 'string' ||
11 typeof value === 'number' ||
12 typeof value === 'boolean' ||
13 typeof value === 'bigint'
14 ) {
15 return String(value)
16 }
17 try {
18 return JSON.stringify(value)
19 } catch {
20 return ''
21 }
22}
23
24export function errorMessage(error: unknown, maxPartLength = 300): string {
25 const parts: Array<string> = []
26 let current: unknown = error
27 while (current !== undefined && current !== null && parts.length < 5) {
28 const text =
29 current instanceof Error ? current.message : describeThrown(current)
30 parts.push(
31 text.length > maxPartLength ? `${text.slice(0, maxPartLength)}…` : text,
32 )
33 current = current instanceof Error ? current.cause : undefined
34 }
35 return parts.join(' ← caused by: ')
36}