diff --git a/src/app/api/v1/rep-dashboard/route.ts b/src/app/api/v1/rep-dashboard/route.ts index 4dabd7b..cccec94 100644 --- a/src/app/api/v1/rep-dashboard/route.ts +++ b/src/app/api/v1/rep-dashboard/route.ts @@ -12,15 +12,48 @@ import { getRepDashboardData } from "@/lib/server/rep-dashboard-service"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; +/** + * Extract the most informative message from a thrown error. + * + * PB SDK errors carry `error.response` (the raw ClientResponseError with + * `message`, `status`, and PB's `data` payload) — when PB rejects a + * filter as 400, the detail often lives in `error.response.data.message` + * or `error.data.message`, NOT in `error.message` (which is a generic + * "Failed to fetch."). Walk the chain so the page UI can show the real + * reason instead of a generic toast. + */ +function extractErrorMessage(error: unknown): string { + if (!(error instanceof Error)) return "Failed to load rep dashboard"; + const anyErr = error as Error & { + status?: number; + data?: { message?: string }; + response?: { message?: string; data?: { message?: string } }; + }; + const status = anyErr.status || anyErr.response?.data ? 400 : 500; + void status; + return ( + anyErr.response?.data?.message || + anyErr.data?.message || + anyErr.response?.message || + anyErr.message || + "Failed to load rep dashboard" + ); +} + export async function GET(req: NextRequest) { try { const sessionUser = await getBrowserSessionUser(req); const data = await getRepDashboardData(sessionUser.id); return NextResponse.json(data); } catch (error) { - const message = - error instanceof Error ? error.message : "Failed to load rep dashboard"; - const status = (error as Error & { status?: number }).status || 500; + // Log the full error server-side so we can diagnose without needing + // the browser devtools open. + console.error("[rep-dashboard] failed:", error); + const message = extractErrorMessage(error); + const status = + (error as Error & { status?: number }).status || + (error as { response?: { status?: number } }).response?.status || + 500; return NextResponse.json({ error: message }, { status }); } } \ No newline at end of file