mirror of
https://github.com/saltbo/zpan.git
synced 2026-08-30 17:50:07 +08:00
d2f3a34f05
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>
21 lines
718 B
TypeScript
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(' <- ')
|
|
}
|