Skip to content

Commit

Permalink
dont error on delete not found
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Dec 31, 2024
1 parent 28b8e3a commit bd0ec3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 11 additions & 4 deletions apps/web/utils/actions/ai-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { setUser } from "@sentry/nextjs";
import { auth } from "@/app/api/auth/[...nextauth]/auth";
import prisma, { isDuplicateError } from "@/utils/prisma";
import prisma, { isDuplicateError, isNotFoundError } from "@/utils/prisma";
import {
RuleType,
ExecutedRuleStatus,
Expand Down Expand Up @@ -422,9 +422,14 @@ export const deleteRuleAction = withActionInstrumentation(
if (rule.userId !== session.user.id)
return { error: "You don't have permission to delete this rule" };

await prisma.rule.delete({
where: { id: ruleId, userId: session.user.id },
});
try {
await prisma.rule.delete({
where: { id: ruleId, userId: session.user.id },
});
} catch (error) {
if (isNotFoundError(error)) return;
throw error;
}
},
);

Expand Down Expand Up @@ -674,6 +679,8 @@ export const saveRulesPromptAction = withActionInstrumentation(
where: { id: rule.rule.id, userId: session.user.id },
});
} catch (error) {
if (isNotFoundError(error)) return;

logger.error("Error deleting rule", {
email: user.email,
ruleId: rule.rule.id,
Expand Down
7 changes: 7 additions & 0 deletions apps/web/utils/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ export function isDuplicateError(error: unknown, key?: string) {

return duplicateError;
}

export function isNotFoundError(error: unknown) {
return (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === "P2025"
);
}

0 comments on commit bd0ec3d

Please sign in to comment.