From 626ca898e4a45186c22a3b624619ddedc2416837 Mon Sep 17 00:00:00 2001 From: Dhravya Shah Date: Sat, 3 Aug 2024 12:36:57 -0700 Subject: [PATCH] handle errors properly --- apps/web/app/api/store/route.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/apps/web/app/api/store/route.ts b/apps/web/app/api/store/route.ts index 12af7894..6efbab8f 100644 --- a/apps/web/app/api/store/route.ts +++ b/apps/web/app/api/store/route.ts @@ -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);