Skip to content

Commit 9b04507

Browse files
committed
Better errors when trying to delete a schedule that doesn’t exist
1 parent 618d220 commit 9b04507

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

apps/webapp/app/routes/api.v1.schedules.$scheduleId.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ActionFunctionArgs, LoaderFunctionArgs } from "@remix-run/server-r
22
import { json } from "@remix-run/server-runtime";
33
import { ScheduleObject, UpdateScheduleOptions } from "@trigger.dev/core/v3";
44
import { z } from "zod";
5-
import { prisma } from "~/db.server";
5+
import { Prisma, prisma } from "~/db.server";
66
import { ViewSchedulePresenter } from "~/presenters/v3/ViewSchedulePresenter.server";
77
import { authenticateApiRequest } from "~/services/apiAuth.server";
88
import { UpsertSchedule } from "~/v3/schedules";
@@ -34,19 +34,34 @@ export async function action({ request, params }: ActionFunctionArgs) {
3434

3535
switch (method) {
3636
case "DELETE": {
37-
const deletedSchedule = await prisma.taskSchedule.delete({
38-
where: {
39-
friendlyId: parsedParams.data.scheduleId,
40-
projectId: authenticationResult.environment.projectId,
41-
},
42-
});
43-
44-
return json(
45-
{
46-
id: deletedSchedule.friendlyId,
47-
},
48-
{ status: 200 }
49-
);
37+
try {
38+
const deletedSchedule = await prisma.taskSchedule.delete({
39+
where: {
40+
friendlyId: parsedParams.data.scheduleId,
41+
projectId: authenticationResult.environment.projectId,
42+
},
43+
});
44+
45+
return json(
46+
{
47+
id: deletedSchedule.friendlyId,
48+
},
49+
{ status: 200 }
50+
);
51+
} catch (error) {
52+
// Check if it's a Prisma error
53+
if (error instanceof Prisma.PrismaClientKnownRequestError) {
54+
return json(
55+
{ error: error.code === "P2025" ? "Schedule not found" : error.message },
56+
{ status: error.code === "P2025" ? 404 : 422 }
57+
);
58+
} else {
59+
return json(
60+
{ error: error instanceof Error ? error.message : "Internal Server Error" },
61+
{ status: 500 }
62+
);
63+
}
64+
}
5065
}
5166
case "PUT": {
5267
const rawBody = await request.json();

0 commit comments

Comments
 (0)