Skip to content

Commit

Permalink
add eror handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 4, 2024
1 parent f374247 commit c2d9810
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions apps/webservice/src/app/api/v1/environments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const POST = request()
),
)
.handle<{ user: User; can: PermissionChecker; body: z.infer<typeof body> }>(
async (ctx) =>
(ctx) =>
ctx.db
.insert(schema.environment)
.values({
Expand All @@ -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 },
),
),
);
}),
);

0 comments on commit c2d9810

Please sign in to comment.