fix(dashboard2): surface PocketBase error details in /api/v1/rep-dashboard
PB SDK rejects filters with a generic "Failed to fetch." wrapper, but the real reason lives in error.response.data.message (or .data.message). Walk the chain so the page's inline error UI shows the actual PB message instead of a useless generic toast. Also log the full error server-side.
This commit is contained in:
parent
6fcef50b3e
commit
42916f2948
1 changed files with 36 additions and 3 deletions
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue