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
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 10m22s
This commit is contained in:
@@ -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(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user