Skip to content

Commit b53d9a3

Browse files
committed
trycatch the AI title JSON parsing
1 parent 32a1ed4 commit b53d9a3

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

apps/webapp/app/routes/resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.query.ai-title.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { openai } from "@ai-sdk/openai";
22
import { json, type ActionFunctionArgs } from "@remix-run/server-runtime";
3+
import { tryCatch } from "@trigger.dev/core/utils";
34
import { z } from "zod";
45
import { prisma } from "~/db.server";
56
import { env } from "~/env.server";
@@ -19,21 +20,33 @@ export async function action({ request, params }: ActionFunctionArgs) {
1920
const { organizationSlug, projectParam, envParam } = EnvironmentParamSchema.parse(params);
2021

2122
// Parse the request body
22-
const data = await request.json();
23+
const [error, data] = await tryCatch(request.json());
24+
if (error) {
25+
return json({ success: false as const, error: error.message, title: null }, { status: 400 });
26+
}
2327
const submission = RequestSchema.safeParse(data);
2428

2529
if (!submission.success) {
26-
return json({ success: false as const, error: "Invalid request data", title: null }, { status: 400 });
30+
return json(
31+
{ success: false as const, error: "Invalid request data", title: null },
32+
{ status: 400 }
33+
);
2734
}
2835

2936
const project = await findProjectBySlug(organizationSlug, projectParam, userId);
3037
if (!project) {
31-
return json({ success: false as const, error: "Project not found", title: null }, { status: 404 });
38+
return json(
39+
{ success: false as const, error: "Project not found", title: null },
40+
{ status: 404 }
41+
);
3242
}
3343

3444
const environment = await findEnvironmentBySlug(project.id, envParam, userId);
3545
if (!environment) {
36-
return json({ success: false as const, error: "Environment not found", title: null }, { status: 404 });
46+
return json(
47+
{ success: false as const, error: "Environment not found", title: null },
48+
{ status: 404 }
49+
);
3750
}
3851

3952
if (!env.OPENAI_API_KEY) {

0 commit comments

Comments
 (0)