From c2d9810f3481e6b4be1ddc36cc056ad5ee53e2ea Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Mon, 4 Nov 2024 12:10:36 -0500 Subject: [PATCH] add eror handling logic --- .../src/app/api/v1/environments/route.ts | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/apps/webservice/src/app/api/v1/environments/route.ts b/apps/webservice/src/app/api/v1/environments/route.ts index b6a1e1db..aadd5554 100644 --- a/apps/webservice/src/app/api/v1/environments/route.ts +++ b/apps/webservice/src/app/api/v1/environments/route.ts @@ -45,7 +45,7 @@ export const POST = request() ), ) .handle<{ user: User; can: PermissionChecker; body: z.infer }>( - async (ctx) => + (ctx) => ctx.db .insert(schema.environment) .values({ @@ -69,10 +69,22 @@ export const POST = request() await createJobsForNewEnvironment(ctx.db, environment); return NextResponse.json({ environment }); }) - .catch(() => - NextResponse.json( + .catch((error) => { + if ( + error.code === "23505" && + error.constraint === "environment_system_id_name_key" + ) { + return NextResponse.json( + { + error: + "An environment with this name already exists in this system.", + }, + { status: 409 }, + ); + } + return NextResponse.json( { error: "Failed to create environment" }, { status: 500 }, - ), - ), + ); + }), );