diff --git a/src/app/api/circles/route.ts b/src/app/api/circles/route.ts index 39c6c17..bf9cac1 100644 --- a/src/app/api/circles/route.ts +++ b/src/app/api/circles/route.ts @@ -34,17 +34,21 @@ export async function POST(request: Request) { return NextResponse.json({ error: "Circle name is required" }, { status: 400 }); } - const [circle] = await sql.unsafe( - `INSERT INTO circles (name, created_by) VALUES ($1, $2) - RETURNING id, name, created_by as "createdBy", created_at as "createdAt"`, - [name.trim(), familyId] - ); + try { + const [circle] = await sql.unsafe( + `INSERT INTO circles (name, created_by) VALUES ($1, $2) + RETURNING id, name, created_by as "createdBy", created_at as "createdAt"`, + [name.trim(), familyId] + ); - // Creator is automatically an admin member - await sql.unsafe( - `INSERT INTO circle_members (circle_id, family_id, role) VALUES ($1, $2, 'admin')`, - [circle.id, familyId] - ); + await sql.unsafe( + `INSERT INTO circle_members (circle_id, family_id, role) VALUES ($1, $2, 'admin')`, + [circle.id, familyId] + ); - return NextResponse.json({ success: true, circle: { ...circle, role: "admin", memberCount: 1 } }); + return NextResponse.json({ success: true, circle: { ...circle, role: "admin", memberCount: 1 } }); + } catch (err: unknown) { + const msg = err instanceof Error ? err.message : String(err); + return NextResponse.json({ error: msg }, { status: 500 }); + } }