Skip to content

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

fix(dashboards): return 404 when deleting a missing widget (#17673)

Max Deichmann74512fcf585e
diff --git a/.agents/skills/code-review/references/review-checklist.md b/.agents/skills/code-review/references/review-checklist.md
index cc2c5d6d94..dfbccb8963 100644
--- a/.agents/skills/code-review/references/review-checklist.md
+++ b/.agents/skills/code-review/references/review-checklist.md
@@ -96,7 +96,7 @@ This is the canonical shared review checklist for Langfuse.
## Prisma errors
-- Do not inline `instanceof Prisma.PrismaClientKnownRequestError && code === "P2025"`. Reuse `isPrismaRecordNotFoundError` in `web` (`web/src/features/analytics-integrations/server/isPrismaRecordNotFoundError.ts`) and `isRecordNotFoundError` in `worker` (`worker/src/features/integrations/prismaErrors.ts`). A true result maps to `LangfuseNotFoundError` (or the domain not-found subclass) so a missing row is a 404, not a 500.
+- Do not inline `instanceof Prisma.PrismaClientKnownRequestError && code === "P2025"`. Reuse `isPrismaRecordNotFoundError` (`packages/shared/src/server/prismaErrors.ts`, re-exported from `@langfuse/shared/src/server`) and `isRecordNotFoundError` in `worker` (`worker/src/features/integrations/prismaErrors.ts`). A true result maps to `LangfuseNotFoundError` (or the domain not-found subclass) so a missing row is a 404, not a 500.
## JavaScript / TypeScript Style
diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.ts
index 6ecd44753e..88edc31d10 100644
--- a/packages/shared/src/server/index.ts
+++ b/packages/shared/src/server/index.ts
@@ -159,6 +159,7 @@ export * from "./utils/metadata_conversion";
export * from "./repositories/experiments";
export * from "./utils/rendering";
export * from "./utils/sqlLike";
+export * from "./prismaErrors";
export * from "./redis/evalExecutionQueue";
export * from "./redis/llmAsJudgeExecutionQueue";
export * from "./redis/codeEvalExecutionQueue";
diff --git a/web/src/features/analytics-integrations/server/isPrismaRecordNotFoundError.ts b/packages/shared/src/server/prismaErrors.ts
similarity index 61%
rename from web/src/features/analytics-integrations/server/isPrismaRecordNotFoundError.ts
rename to packages/shared/src/server/prismaErrors.ts
index cd6299b502..21c71f0e8c 100644
--- a/web/src/features/analytics-integrations/server/isPrismaRecordNotFoundError.ts
+++ b/packages/shared/src/server/prismaErrors.ts
@@ -1,5 +1,6 @@
-import { Prisma } from "@langfuse/shared/src/db";
+import { Prisma } from "../db";
+/** True when Prisma reports that a write matched no row (P2025). */
export const isPrismaRecordNotFoundError = (error: unknown): boolean =>
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === "P2025";
diff --git a/packages/shared/src/server/services/DashboardService/DashboardService.ts b/packages/shared/src/server/services/DashboardService/DashboardService.ts
index 586dd1380c..734d019f30 100644
--- a/packages/shared/src/server/services/DashboardService/DashboardService.ts
+++ b/packages/shared/src/server/services/DashboardService/DashboardService.ts
@@ -1,4 +1,5 @@
import { prisma, Prisma } from "../../../db";
+import { isPrismaRecordNotFoundError } from "../../prismaErrors";
import {
LangfuseConflictError,
LangfuseNotFoundError,
@@ -195,10 +196,7 @@ export class DashboardService {
} catch (e) {
// P2025 = row not found; also thrown for cross-project ids, so the 404
// does not leak whether the dashboard exists in another project.
- if (
- e instanceof Prisma.PrismaClientKnownRequestError &&
- e.code === "P2025"
- ) {
+ if (isPrismaRecordNotFoundError(e)) {
throw new LangfuseNotFoundError(
`Dashboard ${dashboardId} not found in project ${projectId}`,
);
@@ -299,10 +297,7 @@ export class DashboardService {
} catch (e) {
// P2025 = row not found; also thrown for cross-project ids, so the 404
// does not leak whether the dashboard exists in another project.
- if (
- e instanceof Prisma.PrismaClientKnownRequestError &&
- e.code === "P2025"
- ) {
+ if (isPrismaRecordNotFoundError(e)) {
throw new LangfuseNotFoundError(
`Dashboard ${dashboardId} not found in project ${projectId}`,
);
@@ -464,7 +459,8 @@ export class DashboardService {
/**
* Deletes a dashboard widget.
- * Throws an error if the widget is still referenced in any dashboard.
+ * Throws LangfuseConflictError if the widget is still referenced in any
+ * dashboard, and LangfuseNotFoundError if it does not exist in the project.
*/
public static async deleteWidget(
widgetId: string,
@@ -495,13 +491,23 @@ export class DashboardService {
);
}
- // Delete the widget if it's not referenced
- await prisma.dashboardWidget.delete({
- where: {
- id: widgetId,
- projectId,
- },
- });
+ try {
+ await prisma.dashboardWidget.delete({
+ where: {
+ id: widgetId,
+ projectId,
+ },
+ });
+ } catch (e) {
+ // P2025 = row not found; also thrown for cross-project ids, so the 404
+ // does not leak whether the widget exists in another project.
+ if (isPrismaRecordNotFoundError(e)) {
+ throw new LangfuseNotFoundError(
+ `Widget ${widgetId} not found in project ${projectId}`,
+ );
+ }
+ throw e;
+ }
}
/**
diff --git a/web/src/__tests__/server/dashboard-service-delete.servertest.ts b/web/src/__tests__/server/dashboard-service-delete.servertest.ts
index 3b768919e8..534225c6f5 100644
--- a/web/src/__tests__/server/dashboard-service-delete.servertest.ts
+++ b/web/src/__tests__/server/dashboard-service-delete.servertest.ts
@@ -3,7 +3,78 @@ import {
createOrgProjectAndApiKey,
DashboardService,
} from "@langfuse/shared/src/server";
-import { LangfuseNotFoundError } from "@langfuse/shared";
+import { LangfuseNotFoundError, LangfuseConflictError } from "@langfuse/shared";
+import { DashboardWidgetViews, prisma } from "@langfuse/shared/src/db";
+import { appRouter } from "@/src/server/api/root";
+import { createInnerTRPCContext } from "@/src/server/api/trpc";
+import type { Session } from "next-auth";
+import type { TRPCError } from "@trpc/server";
+
+const widgetInput = {
+ name: "delete-me",
+ description: "",
+ view: DashboardWidgetViews.TRACES,
+ dimensions: [{ field: "name" }],
+ metrics: [{ measure: "count", agg: "count" as const }],
+ filters: [],
+ chartType: "LINE_TIME_SERIES" as const,
+ chartConfig: { type: "LINE_TIME_SERIES" as const },
+};
+
+async function createOwnerCaller() {
+ const { project, org } = await createOrgProjectAndApiKey();
+ const session: Session = {
+ expires: "1",
+ user: {
+ id: `widget-delete-user-${uuidv4()}`,
+ canCreateOrganizations: true,
+ name: "Test User",
+ organizations: [
+ {
+ id: org.id,
+ name: org.name,
+ role: "OWNER",
+ plan: "cloud:hobby",
+ cloudConfig: undefined,
+ metadata: {},
+ aiFeaturesEnabled: false,
+ aiTelemetryEnabled: true,
+ projects: [
+ {
+ id: project.id,
+ role: "ADMIN",
+ retentionDays: 30,
+ deletedAt: null,
+ name: project.name,
+ hasTraces: true,
+ metadata: {},
+ createdAt: new Date().toISOString(),
+ },
+ ],
+ },
+ ],
+ featureFlags: {
+ searchBar: false,
+ excludeClickhouseRead: false,
+ templateFlag: true,
+ v4BetaToggleVisible: false,
+ observationEvals: false,
+ experimentsV4Enabled: false,
+ },
+ admin: true,
+ },
+ environment: {
+ enableExperimentalFeatures: false,
+ selfHostedInstancePlan: "cloud:hobby",
+ },
+ };
+
+ const ctx = createInnerTRPCContext({ session, headers: {} });
+ return {
+ projectId: project.id,
+ caller: appRouter.createCaller({ ...ctx, prisma }),
+ };
+}
describe("DashboardService.deleteDashboard", () => {
it("deletes an existing dashboard and throws LangfuseNotFoundError on repeat delete", async () => {
@@ -43,3 +114,111 @@ describe("DashboardService.deleteDashboard", () => {
).rejects.toThrow(LangfuseNotFoundError);
});
});
+
+describe("DashboardService.deleteWidget", () => {
+ it("deletes an existing widget and throws LangfuseNotFoundError on repeat delete", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ const widget = await DashboardService.createWidget(projectId, widgetInput);
+
+ await DashboardService.deleteWidget(widget.id, projectId);
+
+ await expect(
+ DashboardService.deleteWidget(widget.id, projectId),
+ ).rejects.toThrow(LangfuseNotFoundError);
+ });
+
+ it("throws LangfuseNotFoundError for an unknown widget id", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+
+ await expect(
+ DashboardService.deleteWidget(uuidv4(), projectId),
+ ).rejects.toThrow(LangfuseNotFoundError);
+ });
+
+ it("throws LangfuseNotFoundError when the widget belongs to another project", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ const { projectId: otherProjectId } = await createOrgProjectAndApiKey();
+ const widget = await DashboardService.createWidget(
+ otherProjectId,
+ widgetInput,
+ );
+
+ await expect(
+ DashboardService.deleteWidget(widget.id, projectId),
+ ).rejects.toThrow(LangfuseNotFoundError);
+ });
+
+ it("throws LangfuseConflictError when the widget is still on a dashboard", async () => {
+ const { projectId } = await createOrgProjectAndApiKey();
+ const widget = await DashboardService.createWidget(projectId, widgetInput);
+ const dashboard = await DashboardService.createDashboard(
+ projectId,
+ "uses-widget",
+ "",
+ );
+ await DashboardService.updateDashboardDefinition(dashboard.id, projectId, {
+ widgets: [
+ {
+ type: "widget",
+ id: uuidv4(),
+ widgetId: widget.id,
+ x: 0,
+ y: 0,
+ x_size: 6,
+ y_size: 4,
+ },
+ ],
+ });
+
+ await expect(
+ DashboardService.deleteWidget(widget.id, projectId),
+ ).rejects.toThrow(LangfuseConflictError);
+ });
+});
+
+describe("dashboardWidgets.delete", () => {
+ it("returns NOT_FOUND instead of INTERNAL_SERVER_ERROR for a missing widget", async () => {
+ const { projectId, caller } = await createOwnerCaller();
+
+ await expect(
+ caller.dashboardWidgets.delete({
+ projectId,
+ widgetId: uuidv4(),
+ }),
+ ).rejects.toMatchObject({
+ code: "NOT_FOUND",
+ } satisfies Partial<TRPCError>);
+ });
+
+ it("returns CONFLICT when the widget is still on a dashboard", async () => {
+ const { projectId, caller } = await createOwnerCaller();
+ const widget = await DashboardService.createWidget(projectId, widgetInput);
+ const dashboard = await DashboardService.createDashboard(
+ projectId,
+ "uses-widget",
+ "",
+ );
+ await DashboardService.updateDashboardDefinition(dashboard.id, projectId, {
+ widgets: [
+ {
+ type: "widget",
+ id: uuidv4(),
+ widgetId: widget.id,
+ x: 0,
+ y: 0,
+ x_size: 6,
+ y_size: 4,
+ },
+ ],
+ });
+
+ await expect(
+ caller.dashboardWidgets.delete({
+ projectId,
+ widgetId: widget.id,
+ }),
+ ).rejects.toMatchObject({
+ code: "CONFLICT",
+ } satisfies Partial<TRPCError>);
+ });
+});
diff --git a/web/src/features/analytics-integrations/server/index.ts b/web/src/features/analytics-integrations/server/index.ts
index bcbf669f9d..c09d7989f8 100644
--- a/web/src/features/analytics-integrations/server/index.ts
+++ b/web/src/features/analytics-integrations/server/index.ts
@@ -5,4 +5,4 @@ export {
assertPersistedExportSourceAllowed,
resolveExportSource,
} from "@/src/features/analytics-integrations/server/exportSource";
-export { isPrismaRecordNotFoundError } from "@/src/features/analytics-integrations/server/isPrismaRecordNotFoundError";
+export { isPrismaRecordNotFoundError } from "@langfuse/shared/src/server";
diff --git a/web/src/features/annotation-queues/server/annotationQueuesRouter.ts b/web/src/features/annotation-queues/server/annotationQueuesRouter.ts
index a298a5d243..2223884834 100644
--- a/web/src/features/annotation-queues/server/annotationQueuesRouter.ts
+++ b/web/src/features/annotation-queues/server/annotationQueuesRouter.ts
@@ -1,5 +1,4 @@
import { env } from "@/src/env.mjs";
-import { isPrismaRecordNotFoundError } from "@/src/features/analytics-integrations/server/isPrismaRecordNotFoundError";
import { auditLog } from "@/src/features/audit-logs/server";
import { throwIfNoProjectAccess } from "@/src/features/rbac";
import {
@@ -19,6 +18,7 @@ import {
import {
getObservationById,
getObservationByIdFromEventsTable,
+ isPrismaRecordNotFoundError,
logger,
} from "@langfuse/shared/src/server";
import { TRPCError } from "@trpc/server";
diff --git a/web/src/features/datasets/server/actions/createDataset.ts b/web/src/features/datasets/server/actions/createDataset.ts
index fc8f5327b5..cc6a080f6b 100644
--- a/web/src/features/datasets/server/actions/createDataset.ts
+++ b/web/src/features/datasets/server/actions/createDataset.ts
@@ -7,8 +7,10 @@ import {
Prisma,
} from "@langfuse/shared";
import { prisma } from "@langfuse/shared/src/db";
-import { validateAllDatasetItems } from "@langfuse/shared/src/server";
-import { isPrismaRecordNotFoundError } from "@/src/features/analytics-integrations/server/isPrismaRecordNotFoundError";
+import {
+ isPrismaRecordNotFoundError,
+ validateAllDatasetItems,
+} from "@langfuse/shared/src/server";
type DatasetJson =
| Prisma.InputJsonObject
diff --git a/web/src/server/api/routers/dashboardWidgets.ts b/web/src/server/api/routers/dashboardWidgets.ts
index efbee8267a..948b8da3c9 100644
--- a/web/src/server/api/routers/dashboardWidgets.ts
+++ b/web/src/server/api/routers/dashboardWidgets.ts
@@ -7,7 +7,6 @@ import {
orderBy,
singleFilterList,
optionalPaginationZod,
- LangfuseConflictError,
} from "@langfuse/shared";
import { throwIfNoProjectAccess } from "@/src/features/rbac";
import { DashboardWidgetChartType } from "@langfuse/shared/src/db";
@@ -209,25 +208,10 @@ export const dashboardWidgetRouter = createTRPCRouter({
scope: "dashboards:CUD",
});
- try {
- // Delete the widget using the DashboardService
- await DashboardService.deleteWidget(input.widgetId, input.projectId);
-
- return {
- success: true,
- };
- } catch (error) {
- // If the widget is still referenced in dashboards, throw a CONFLICT error
- if (error instanceof LangfuseConflictError) {
- throw new TRPCError({
- code: "CONFLICT",
- message: error.message,
- });
- }
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: (error as Error)?.message,
- });
- }
+ await DashboardService.deleteWidget(input.widgetId, input.projectId);
+
+ return {
+ success: true,
+ };
}),
});