Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(integrations): return not found for repeated deletes (#15924)

Max Deichmann4e6877e12e6f
diff --git a/web/src/__tests__/server/mixpanel-integration.servertest.ts b/web/src/__tests__/server/mixpanel-integration.servertest.ts
index dad70854a7..b991af65ec 100644
--- a/web/src/__tests__/server/mixpanel-integration.servertest.ts
+++ b/web/src/__tests__/server/mixpanel-integration.servertest.ts
@@ -1,5 +1,5 @@
import type { Session } from "next-auth";
-import { prisma } from "@langfuse/shared/src/db";
+import { prisma, Prisma } from "@langfuse/shared/src/db";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
@@ -54,6 +54,35 @@ const buildSession = (orgId: string, projectId: string): Session => ({
environment: {} as any,
});
+describe("Mixpanel Integration delete", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("returns NOT_FOUND when the integration is already absent", async () => {
+ const { project, orgId } = await createOrgProjectAndApiKey();
+ const ctx = createInnerTRPCContext({
+ session: buildSession(orgId, project.id),
+ headers: {},
+ });
+ const caller = appRouter.createCaller({ ...ctx, prisma });
+
+ vi.spyOn(prisma.mixpanelIntegration, "delete").mockRejectedValueOnce(
+ new Prisma.PrismaClientKnownRequestError(
+ "Record to delete does not exist.",
+ {
+ code: "P2025",
+ clientVersion: "test",
+ },
+ ),
+ );
+
+ await expect(
+ caller.mixpanelIntegration.delete({ projectId: project.id }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+});
+
describe("Mixpanel Integration legacy export source cutoff gate", () => {
const originalEncryptionKey = process.env.ENCRYPTION_KEY;
diff --git a/web/src/__tests__/server/posthog-integration.servertest.ts b/web/src/__tests__/server/posthog-integration.servertest.ts
index 996100eb91..d04d304e5d 100644
--- a/web/src/__tests__/server/posthog-integration.servertest.ts
+++ b/web/src/__tests__/server/posthog-integration.servertest.ts
@@ -1,5 +1,5 @@
import type { Session } from "next-auth";
-import { prisma } from "@langfuse/shared/src/db";
+import { prisma, Prisma } from "@langfuse/shared/src/db";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
@@ -95,6 +95,35 @@ describe("PostHog Integration SSRF Protection", () => {
});
});
+describe("PostHog Integration delete", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("returns NOT_FOUND when the integration is already absent", async () => {
+ const { project, orgId } = await createOrgProjectAndApiKey();
+ const ctx = createInnerTRPCContext({
+ session: buildSession(orgId, project.id),
+ headers: {},
+ });
+ const caller = appRouter.createCaller({ ...ctx, prisma });
+
+ vi.spyOn(prisma.posthogIntegration, "delete").mockRejectedValueOnce(
+ new Prisma.PrismaClientKnownRequestError(
+ "Record to delete does not exist.",
+ {
+ code: "P2025",
+ clientVersion: "test",
+ },
+ ),
+ );
+
+ await expect(
+ caller.posthogIntegration.delete({ projectId: project.id }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+});
+
describe("PostHog Integration legacy export source cutoff gate", () => {
const originalEncryptionKey = process.env.ENCRYPTION_KEY;
diff --git a/web/src/features/mixpanel-integration/mixpanel-integration-router.ts b/web/src/features/mixpanel-integration/mixpanel-integration-router.ts
index 3c5955c702..0a2baf6198 100644
--- a/web/src/features/mixpanel-integration/mixpanel-integration-router.ts
+++ b/web/src/features/mixpanel-integration/mixpanel-integration-router.ts
@@ -16,8 +16,14 @@ import {
AnalyticsIntegrationExportSource,
areLegacyWritesActive,
InvalidRequestError,
+ LangfuseNotFoundError,
validateExportSource,
} from "@langfuse/shared";
+import { Prisma } from "@langfuse/shared/src/db";
+
+const isMixpanelIntegrationNotFoundError = (error: unknown): boolean =>
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ error.code === "P2025";
export const mixpanelIntegrationRouter = createTRPCRouter({
get: protectedProjectProcedure
@@ -207,29 +213,30 @@ export const mixpanelIntegrationRouter = createTRPCRouter({
delete: protectedProjectProcedure
.input(z.object({ projectId: z.string() }))
.mutation(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "integrations:CRUD",
- });
- await auditLog({
- session: ctx.session,
- action: "delete",
- resourceType: "mixpanelIntegration",
- resourceId: input.projectId,
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "integrations:CRUD",
+ });
+ await auditLog({
+ session: ctx.session,
+ action: "delete",
+ resourceType: "mixpanelIntegration",
+ resourceId: input.projectId,
+ });
+ try {
await ctx.prisma.mixpanelIntegration.delete({
where: {
projectId: input.projectId,
},
});
- } catch (e) {
- console.log("mixpanel integration delete", e);
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- });
+ } catch (error) {
+ if (isMixpanelIntegrationNotFoundError(error)) {
+ throw new LangfuseNotFoundError("Mixpanel integration not found");
+ }
+
+ throw error;
}
}),
});
diff --git a/web/src/features/posthog-integration/posthog-integration-router.ts b/web/src/features/posthog-integration/posthog-integration-router.ts
index 5a353a8b45..f678eff626 100644
--- a/web/src/features/posthog-integration/posthog-integration-router.ts
+++ b/web/src/features/posthog-integration/posthog-integration-router.ts
@@ -17,8 +17,14 @@ import {
AnalyticsIntegrationExportSource,
areLegacyWritesActive,
InvalidRequestError,
+ LangfuseNotFoundError,
validateExportSource,
} from "@langfuse/shared";
+import { Prisma } from "@langfuse/shared/src/db";
+
+const isPostHogIntegrationNotFoundError = (error: unknown): boolean =>
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ error.code === "P2025";
export const posthogIntegrationRouter = createTRPCRouter({
get: protectedProjectProcedure
@@ -220,29 +226,30 @@ export const posthogIntegrationRouter = createTRPCRouter({
delete: protectedProjectProcedure
.input(z.object({ projectId: z.string() }))
.mutation(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "integrations:CRUD",
- });
- await auditLog({
- session: ctx.session,
- action: "delete",
- resourceType: "posthogIntegration",
- resourceId: input.projectId,
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "integrations:CRUD",
+ });
+ await auditLog({
+ session: ctx.session,
+ action: "delete",
+ resourceType: "posthogIntegration",
+ resourceId: input.projectId,
+ });
+ try {
await ctx.prisma.posthogIntegration.delete({
where: {
projectId: input.projectId,
},
});
- } catch (e) {
- console.log("posthog integration delete", e);
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- });
+ } catch (error) {
+ if (isPostHogIntegrationNotFoundError(error)) {
+ throw new LangfuseNotFoundError("PostHog integration not found");
+ }
+
+ throw error;
}
}),
});