Every app/api route handler now goes through withRouteLogging, which logs a correlated reqId/method/path/status/duration for each request and turns any uncaught error into a logged stack trace plus a clean JSON 500 instead of Next's default opaque failure. proxy.ts logs rejected auth attempts, and instrumentation.ts's onRequestError catches anything that still escapes a route handler. Also adds a minimal not-found page matching the app's card styling. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
57 lines
2.3 KiB
TypeScript
57 lines
2.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { deletePlaySession, endPlaySession, getPlaySessionDetail } from "@/lib/db/queries/play-sessions";
|
|
import { getRecordingsForSession } from "@/lib/db/queries/recordings";
|
|
import { withRouteLogging } from "@/lib/api/with-route-logging";
|
|
import { createLogger } from "@/lib/logger";
|
|
|
|
const log = createLogger("play-sessions");
|
|
const patchSchema = z.object({ status: z.enum(["completed", "aborted"]) });
|
|
|
|
export const GET = withRouteLogging(
|
|
"play-sessions.get",
|
|
async (_req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params;
|
|
const detail = await getPlaySessionDetail(Number(id));
|
|
if (!detail) return NextResponse.json({ error: "not found" }, { status: 404 });
|
|
return NextResponse.json(detail);
|
|
},
|
|
);
|
|
|
|
export const PATCH = withRouteLogging(
|
|
"play-sessions.end",
|
|
async (req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params;
|
|
const parsed = patchSchema.safeParse(await req.json().catch(() => null));
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "status must be completed or aborted" }, { status: 400 });
|
|
}
|
|
const updated = await endPlaySession(Number(id), parsed.data);
|
|
if (!updated) return NextResponse.json({ error: "not found" }, { status: 404 });
|
|
return NextResponse.json({ playSession: updated });
|
|
},
|
|
);
|
|
|
|
export const DELETE = withRouteLogging(
|
|
"play-sessions.delete",
|
|
async (req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params;
|
|
const cascade = new URL(req.url).searchParams.get("cascade") === "true";
|
|
try {
|
|
await deletePlaySession(Number(id), { cascade });
|
|
return NextResponse.json({ ok: true });
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
if (message.includes("FOREIGN KEY") || message.includes("SQLITE_CONSTRAINT")) {
|
|
log.warn("blocked session delete due to referencing recording", { sessionId: id, cascade });
|
|
const blockingRecordings = await getRecordingsForSession(Number(id));
|
|
return NextResponse.json(
|
|
{ error: "cannot delete a session that a saved recording still references", recordings: blockingRecordings },
|
|
{ status: 409 },
|
|
);
|
|
}
|
|
throw err;
|
|
}
|
|
},
|
|
);
|