From 21c0a7b59c74467c0e9c5ad20e5f768168d0b37d Mon Sep 17 00:00:00 2001 From: "groombook-paperclip[bot]" <268890960+groombook-paperclip[bot]@users.noreply.github.com> Date: Wed, 18 Mar 2026 13:36:31 +0000 Subject: [PATCH] fix(reports): fix churn query crash and improve error reporting (#51) The /api/reports/clients endpoint was crashing with a 500 on every request because a raw JavaScript Date passed into a sql template literal in .having() cannot be serialized by postgres-js. The fix serializes it as an ISO string with an explicit ::timestamptz cast. Also adds reportsRouter.onError() and improves the frontend error message to surface which specific endpoint failed and why. Fixes #49 Co-Authored-By: Paperclip --- apps/api/src/routes/reports.ts | 5 +++++ apps/web/src/pages/Reports.tsx | 19 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/api/src/routes/reports.ts b/apps/api/src/routes/reports.ts index 3d53bda..8be162b 100644 --- a/apps/api/src/routes/reports.ts +++ b/apps/api/src/routes/reports.ts @@ -16,6 +16,11 @@ import { export const reportsRouter = new Hono(); +reportsRouter.onError((err, c) => { + console.error("[reports] unhandled error:", err); + return c.json({ error: "Internal server error", message: err.message }, 500); +}); + // ─── Helpers ────────────────────────────────────────────────────────────────── function parseDate(value: string | undefined, fallback: Date): Date { diff --git a/apps/web/src/pages/Reports.tsx b/apps/web/src/pages/Reports.tsx index 40d0087..fabb159 100644 --- a/apps/web/src/pages/Reports.tsx +++ b/apps/web/src/pages/Reports.tsx @@ -176,8 +176,23 @@ export function ReportsPage() { fetch(`/api/reports/clients?${qs}`), ]); - if (!summRes.ok || !revRes.ok || !apptRes.ok || !svcRes.ok || !clientRes.ok) { - throw new Error("Failed to load report data"); + const failures = [ + ["summary", summRes], + ["revenue", revRes], + ["appointments", apptRes], + ["services", svcRes], + ["clients", clientRes], + ].filter(([, r]) => !(r as Response).ok); + if (failures.length > 0) { + const details = await Promise.all( + failures.map(async ([name, r]) => { + const res = r as Response; + let body = ""; + try { body = await res.text(); } catch { /* ignore */ } + return `${name} (HTTP ${res.status}${body ? `: ${body.slice(0, 120)}` : ""})`; + }) + ); + throw new Error(`Failed to load report data — ${details.join(", ")}`); } const [summData, revData, apptData, svcData, clientData] = await Promise.all([