🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(dashboards): return 404 instead of 500 when updating a missing dashboard (#15454)
Lysander Kiesel2d6f60b225c7
diff --git a/packages/shared/src/server/services/DashboardService/DashboardService.ts b/packages/shared/src/server/services/DashboardService/DashboardService.tsindex 2c327d4e40..76c0b9809e 100644--- a/packages/shared/src/server/services/DashboardService/DashboardService.ts+++ b/packages/shared/src/server/services/DashboardService/DashboardService.ts@@ -102,6 +102,42 @@ export class DashboardService {});}+ /**+ * Updates a project-owned dashboard, translating Prisma's P2025 into a 404.+ */+ private static async updateDashboardRecord(+ dashboardId: string,+ projectId: string,+ data: Prisma.DashboardUncheckedUpdateInput,+ ): Promise<DashboardDomain> {+ try {+ const updatedDashboard = await prisma.dashboard.update({+ where: {+ id: dashboardId,+ projectId,+ },+ data,+ });++ return DashboardDomainSchema.parse({+ ...updatedDashboard,+ owner: updatedDashboard.projectId ? "PROJECT" : "LANGFUSE",+ });+ } 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"+ ) {+ throw new LangfuseNotFoundError(+ `Dashboard ${dashboardId} not found in project ${projectId}`,+ );+ }+ throw e;+ }+ }+/*** Updates a dashboard's definition.*/@@ -111,24 +147,13 @@ export class DashboardService {definition: z.infer<typeof DashboardDefinitionSchema>,userId?: string,): Promise<DashboardDomain> {- const updatedDashboard = await prisma.dashboard.update({- where: {- id: dashboardId,- projectId,+ return this.updateDashboardRecord(dashboardId, projectId, {+ updatedBy: userId,+ definition: {+ // Already sanitized: the input is parsed against+ // DashboardDefinitionSchema, which strips unknown keys.+ widgets: definition.widgets,},- data: {- updatedBy: userId,- definition: {- // Already sanitized: the input is parsed against- // DashboardDefinitionSchema, which strips unknown keys.- widgets: definition.widgets,- },- },- });-- return DashboardDomainSchema.parse({- ...updatedDashboard,- owner: updatedDashboard.projectId ? "PROJECT" : "LANGFUSE",});}@@ -142,21 +167,10 @@ export class DashboardService {description: string,userId?: string,): Promise<DashboardDomain> {- const updatedDashboard = await prisma.dashboard.update({- where: {- id: dashboardId,- projectId,- },- data: {- name,- description,- updatedBy: userId,- },- });-- return DashboardDomainSchema.parse({- ...updatedDashboard,- owner: updatedDashboard.projectId ? "PROJECT" : "LANGFUSE",+ return this.updateDashboardRecord(dashboardId, projectId, {+ name,+ description,+ updatedBy: userId,});}@@ -169,20 +183,9 @@ export class DashboardService {filters: z.infer<typeof singleFilter>[],userId?: string,): Promise<DashboardDomain> {- const updatedDashboard = await prisma.dashboard.update({- where: {- id: dashboardId,- projectId,- },- data: {- updatedBy: userId,- filters,- },- });-- return DashboardDomainSchema.parse({- ...updatedDashboard,- owner: updatedDashboard.projectId ? "PROJECT" : "LANGFUSE",+ return this.updateDashboardRecord(dashboardId, projectId, {+ updatedBy: userId,+ filters,});}diff --git a/web/src/__tests__/server/dashboard-service-update.servertest.ts b/web/src/__tests__/server/dashboard-service-update.servertest.tsnew file mode 100644index 0000000000..afc1cccf92--- /dev/null+++ b/web/src/__tests__/server/dashboard-service-update.servertest.ts@@ -0,0 +1,27 @@+import { v4 as uuidv4 } from "uuid";+import {+ createOrgProjectAndApiKey,+ DashboardService,+} from "@langfuse/shared/src/server";+import { LangfuseNotFoundError } from "@langfuse/shared";++describe("DashboardService update methods", () => {+ it("throw LangfuseNotFoundError instead of P2025 for a missing dashboard", async () => {+ const { projectId } = await createOrgProjectAndApiKey();+ const missingId = uuidv4();++ await expect(+ DashboardService.updateDashboardDefinition(missingId, projectId, {+ widgets: [],+ }),+ ).rejects.toThrow(LangfuseNotFoundError);++ await expect(+ DashboardService.updateDashboard(missingId, projectId, "name", ""),+ ).rejects.toThrow(LangfuseNotFoundError);++ await expect(+ DashboardService.updateDashboardFilters(missingId, projectId, []),+ ).rejects.toThrow(LangfuseNotFoundError);+ });+});