Skip to content

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

fix(web): return not found when cancelling a missing batch export (#17676)

Max Deichmann306a8a238d08
diff --git a/web/src/__tests__/server/batchExport-trpc.servertest.ts b/web/src/__tests__/server/batchExport-trpc.servertest.ts
index f0c504fc87..9a338e48d2 100644
--- a/web/src/__tests__/server/batchExport-trpc.servertest.ts
+++ b/web/src/__tests__/server/batchExport-trpc.servertest.ts
@@ -3,6 +3,7 @@ import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { prisma, type Role } from "@langfuse/shared/src/db";
import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
+import { randomUUID } from "crypto";
import type { Session } from "next-auth";
import {
BatchExportFileFormat,
@@ -460,3 +461,119 @@ describe("batchExport tRPC – useEventsTable snapshot", () => {
});
});
});
+
+describe("batchExport tRPC – cancel", () => {
+ afterAll(async () => {
+ await prisma.organization.deleteMany({
+ where: { id: { in: __orgIds } },
+ });
+ });
+
+ it("returns NOT_FOUND when the export is missing instead of a Prisma update error", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ projectRole: "OWNER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ caller.batchExport.cancel({
+ projectId: project.id,
+ batchExportId: randomUUID(),
+ }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+
+ it("returns NOT_FOUND when the export belongs to another project", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ const other = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id, other.org.id);
+
+ const exportJob = await prisma.batchExport.create({
+ data: {
+ projectId: other.project.id,
+ userId: "user-test",
+ status: BatchExportStatus.QUEUED,
+ name: "other project export",
+ format: BatchExportFileFormat.CSV,
+ query: {
+ tableName: BatchTableNames.Traces,
+ filter: null,
+ orderBy: null,
+ },
+ },
+ });
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ projectRole: "OWNER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ caller.batchExport.cancel({
+ projectId: project.id,
+ batchExportId: exportJob.id,
+ }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+
+ const stillQueued = await prisma.batchExport.findUnique({
+ where: { id: exportJob.id },
+ });
+ expect(stillQueued?.status).toBe(BatchExportStatus.QUEUED);
+ expect(stillQueued?.projectId).toBe(other.project.id);
+ });
+
+ it("cancels an existing queued export", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ const exportJob = await prisma.batchExport.create({
+ data: {
+ projectId: project.id,
+ userId: "user-test",
+ status: BatchExportStatus.QUEUED,
+ name: "queued export",
+ format: BatchExportFileFormat.CSV,
+ query: {
+ tableName: BatchTableNames.Traces,
+ filter: null,
+ orderBy: null,
+ },
+ },
+ });
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ projectRole: "OWNER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ caller.batchExport.cancel({
+ projectId: project.id,
+ batchExportId: exportJob.id,
+ }),
+ ).resolves.toBeUndefined();
+
+ const cancelled = await prisma.batchExport.findUnique({
+ where: { id: exportJob.id },
+ });
+ expect(cancelled?.status).toBe(BatchExportStatus.CANCELLED);
+ });
+});
diff --git a/web/src/features/batch-exports/server/batchExport.ts b/web/src/features/batch-exports/server/batchExport.ts
index aec40c70ed..177f442900 100644
--- a/web/src/features/batch-exports/server/batchExport.ts
+++ b/web/src/features/batch-exports/server/batchExport.ts
@@ -174,10 +174,13 @@ export const batchExportRouter = createTRPCRouter({
scope: "batchExports:create",
});
- await ctx.prisma.batchExport.update({
+ const { count } = await ctx.prisma.batchExport.updateMany({
where: { id: input.batchExportId, projectId: input.projectId },
data: { status: BatchExportStatus.CANCELLED },
});
+ if (count === 0) {
+ throw new LangfuseNotFoundError("Batch export not found");
+ }
}),
downloadUrl: protectedProjectProcedure
.input(