From 117ad2d0cef7a31257321704be82e28d26762f34 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Sat, 3 Jan 2026 22:06:28 +0100 Subject: [PATCH] fix: enhance error handling in read progress update by validating request body and returning appropriate error responses --- .../books/[bookId]/read-progress/route.ts | 33 ++++++++++++++++++- tsconfig.json | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/app/api/komga/books/[bookId]/read-progress/route.ts b/src/app/api/komga/books/[bookId]/read-progress/route.ts index b943774..ae5875b 100644 --- a/src/app/api/komga/books/[bookId]/read-progress/route.ts +++ b/src/app/api/komga/books/[bookId]/read-progress/route.ts @@ -11,9 +11,40 @@ export async function PATCH( { params }: { params: Promise<{ bookId: string }> } ) { try { - const { page, completed } = await request.json(); const bookId: string = (await params).bookId; + // Handle empty or invalid body (can happen when request is aborted during navigation) + let body: { page?: unknown; completed?: boolean }; + try { + const text = await request.text(); + if (!text) { + return NextResponse.json( + { + error: { + code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR, + name: "Progress update error", + message: "Empty request body", + }, + }, + { status: 400 } + ); + } + body = JSON.parse(text); + } catch { + return NextResponse.json( + { + error: { + code: ERROR_CODES.BOOK.PROGRESS_UPDATE_ERROR, + name: "Progress update error", + message: "Invalid JSON body", + }, + }, + { status: 400 } + ); + } + + const { page, completed } = body; + if (typeof page !== "number") { return NextResponse.json( { diff --git a/tsconfig.json b/tsconfig.json index e59724b..1bf4017 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,5 +23,5 @@ } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], - "exclude": ["node_modules"] + "exclude": ["node_modules", "temp"] }