Wrap /api/pdf handler in try/catch for JSON error contract

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-06 12:39:01 +05:00
parent c50a295290
commit 9ad4d762d7
+3 -2
View File
@@ -35,6 +35,7 @@ async function resolveUserId(req: NextRequest): Promise<string | null> {
} }
export async function GET(req: NextRequest) { export async function GET(req: NextRequest) {
try {
const userId = await resolveUserId(req); const userId = await resolveUserId(req);
if (!userId) return jsonError(401, "Нужен API-ключ или вход в аккаунт школы"); if (!userId) return jsonError(401, "Нужен API-ключ или вход в аккаунт школы");
@@ -46,7 +47,7 @@ export async function GET(req: NextRequest) {
if (!parsed.success) return jsonError(422, "Проверьте параметры: url, format (A4|Letter), theme (light|dark)"); if (!parsed.success) return jsonError(422, "Проверьте параметры: url, format (A4|Letter), theme (light|dark)");
if ((await getBurstUsage(userId)) >= BURST_LIMIT) { if ((await getBurstUsage(userId)) >= BURST_LIMIT) {
return jsonError(429, "Слишком часто: подождите минуту"); return jsonError(429, "Слишком часто: подождите минуту", { "X-Max-Uses": String(getMonthlyLimit()) });
} }
const limit = getMonthlyLimit(); const limit = getMonthlyLimit();
const used = await getMonthlyUsage(userId); const used = await getMonthlyUsage(userId);
@@ -56,7 +57,6 @@ export async function GET(req: NextRequest) {
}); });
} }
try {
const { pdf, title } = await generateCleanPdf(parsed.data); const { pdf, title } = await generateCleanPdf(parsed.data);
await prisma.toolUsage.create({ data: { userId, tool: PDF_TOOL_ID } }); await prisma.toolUsage.create({ data: { userId, tool: PDF_TOOL_ID } });
@@ -75,6 +75,7 @@ export async function GET(req: NextRequest) {
return jsonError(422, e.message); return jsonError(422, e.message);
} }
if (e instanceof RenderError) { if (e instanceof RenderError) {
console.error("[clean-pdf] render", e);
return jsonError(504, "Не получилось отрендерить страницу, попробуйте позже"); return jsonError(504, "Не получилось отрендерить страницу, попробуйте позже");
} }
console.error("[clean-pdf]", e); console.error("[clean-pdf]", e);