handle errors properly

This commit is contained in:
Dhravya Shah 2024-08-03 12:36:57 -07:00
parent 76d435de89
commit 626ca898e4

View file

@ -16,7 +16,21 @@ export async function POST(req: NextRequest) {
return new Response("Missing BACKEND_SECURITY_KEY", { status: 500 });
}
const body = await req.json();
let body;
try {
body = await req.json();
} catch (e) {
const error = (e as Error).message;
return new Response(
JSON.stringify({
message: "A JSON was not sent to the API. " + error,
}),
{
status: 400,
},
);
}
const validated = addFromAPIType.safeParse(body);