fix: enhance error handling in read progress update by validating request body and returning appropriate error responses
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 10m22s

This commit is contained in:
Julien Froidefond
2026-01-03 22:06:28 +01:00
parent 0d7d27ef82
commit 117ad2d0ce
2 changed files with 33 additions and 2 deletions

View File

@@ -11,9 +11,40 @@ export async function PATCH(
{ params }: { params: Promise<{ bookId: string }> } { params }: { params: Promise<{ bookId: string }> }
) { ) {
try { try {
const { page, completed } = await request.json();
const bookId: string = (await params).bookId; 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") { if (typeof page !== "number") {
return NextResponse.json( return NextResponse.json(
{ {

View File

@@ -23,5 +23,5 @@
} }
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"] "exclude": ["node_modules", "temp"]
} }