From 824d1cc7a627e9a6341e76f063fb8f6298ff2d83 Mon Sep 17 00:00:00 2001 From: Mohd Jami Date: Fri, 3 Jan 2025 22:26:46 +0530 Subject: [PATCH] Refactored error handling --- app/api/delete-itinerary/route.ts | 6 +++++- app/api/generate/route.ts | 12 ++++-------- package.json | 3 +++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/api/delete-itinerary/route.ts b/app/api/delete-itinerary/route.ts index e7be92a..ec5d500 100644 --- a/app/api/delete-itinerary/route.ts +++ b/app/api/delete-itinerary/route.ts @@ -6,7 +6,11 @@ export async function DELETE(req: NextRequest) { try { const { id } = await req.json(); const supabase = createClient(); - const response = await supabase.from("response").delete().match({ id }); + const {data: response, error } = await supabase.from("response").delete().match({ id }); + if(error){ + console.log(error); + return NextResponse.json({ error: error.message }, { status: 400 }); + } revalidatePath("/"); return NextResponse.json({ message: "Itinerary deleted successfully" }); } catch (error) { diff --git a/app/api/generate/route.ts b/app/api/generate/route.ts index ba7326c..b6e7b7c 100644 --- a/app/api/generate/route.ts +++ b/app/api/generate/route.ts @@ -26,12 +26,10 @@ export async function POST(req: Request, res: Response) { budget, interests, } = await req.json(); - const [supabase, user] = await Promise.all([ - createClient(), - getServerUser(), - ]); + const user = await getServerUser() - if (!user) { + + if (!user) { return NextResponse.json( { error: "User not found", @@ -102,7 +100,6 @@ export async function POST(req: Request, res: Response) { .replaceAll("```json", "") .replaceAll("```", ""); const json = JSON.parse(text); - console.log(json); const name = `${currentLocation.split(",")[0]} to ${ travelLocation.split(",")[0] }`; @@ -111,7 +108,7 @@ export async function POST(req: Request, res: Response) { //Insert these details into user preferneces table in supabase //not critical push them to the queue - const res = await client.publishJSON({ + await client.publishJSON({ url: `${process.env.NEXT_PUBLIC_URL}/api/process`, body: { currentLocation, @@ -125,7 +122,6 @@ export async function POST(req: Request, res: Response) { json, }, }); - console.log("userid", user.id); revalidatePath("/"); return NextResponse.json({ itinerary: json }); } catch (error) { diff --git a/package.json b/package.json index 4d33db1..a244eb1 100644 --- a/package.json +++ b/package.json @@ -58,5 +58,8 @@ "postcss": "^8", "tailwindcss": "^3.4.1", "typescript": "^5" + }, + "volta": { + "node": "20.18.0" } }