From 6624690f39a60e1559647ff1e1151ac6abfd189b Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 14 Jul 2023 11:19:38 +0100 Subject: [PATCH] udpated structure of create portfolio api --- src/app/api/portfolio/route.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/app/api/portfolio/route.ts b/src/app/api/portfolio/route.ts index cf8303ca..2b4ad942 100644 --- a/src/app/api/portfolio/route.ts +++ b/src/app/api/portfolio/route.ts @@ -20,9 +20,18 @@ const createPortfolioSchema = z.object({ export async function POST(request: NextRequest) { const body = await request.json(); + let validatedBody; + + try { + validatedBody = createPortfolioSchema.parse(body); + } catch (error) { + console.error("Invalid input: ", error); + return new NextResponse(JSON.stringify({ msg: "Invalid input" }), { + status: 400, + }); + } try { - const validatedBody = createPortfolioSchema.parse(body); const { userId, portfolioName, budget, goal, status, role } = validatedBody; const creationDate = new Date(); @@ -52,6 +61,8 @@ export async function POST(request: NextRequest) { return new NextResponse(JSON.stringify(response[0]), { status: 201 }); } catch (error) { console.error(error); - throw error; + return new NextResponse(JSON.stringify({ msg: "Insternal server error" }), { + status: 500, + }); } }