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 <noreply@paperclip.ing>
This commit was merged in pull request #51.
This commit is contained in:
groombook-paperclip[bot]
2026-03-18 13:36:31 +00:00
committed by GitHub
parent 639429d73d
commit 21c0a7b59c
2 changed files with 22 additions and 2 deletions
+5
View File
@@ -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 {
+17 -2
View File
@@ -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([