Skip to content

Comments

fix: more precise error code for event type duplication handler#23059

Merged
hbjORbj merged 6 commits intomainfrom
fix/false-500-from-duplication-error
Aug 14, 2025
Merged

fix: more precise error code for event type duplication handler#23059
hbjORbj merged 6 commits intomainfrom
fix/false-500-from-duplication-error

Conversation

@hbjORbj
Copy link
Contributor

@hbjORbj hbjORbj commented Aug 13, 2025

What does this PR do?

We can see this error quite a number of times

Something went wrong Error [TRPCError]: Error duplicating event type PrismaClientKnownRequestError: Invalid `prisma.eventType.create()` invocation: Unique constraint failed on the fields: (`userId`,`slug`)

It's being conceived as error code 500 when it should be 400.

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A - I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • I added an unit test

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 13, 2025

Walkthrough

The duplicate event-type handler's error handling was changed: the catch block now detects Prisma.PrismaClientKnownRequestError with code "P2002" and throws a TRPCError with code "BAD_REQUEST" and message "Unique constraint violation while creating a duplicate event." Other errors are thrown as TRPCError with code "INTERNAL_SERVER_ERROR" and the original error message. A unit test was added that mocks Prisma and EventTypeRepository to assert the BAD_REQUEST mapping when a P2002 error is raised. No public/exported signatures were changed.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/false-500-from-duplication-error

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@graphite-app graphite-app bot requested a review from a team August 13, 2025 09:42
@keithwillcode keithwillcode added core area: core, team members only foundation labels Aug 13, 2025
@dosubot dosubot bot added event-types area: event types, event-types 🐛 bug Something isn't working labels Aug 13, 2025
@hbjORbj hbjORbj changed the title fix: false 500 from duplication error fix: improve error handling for event type duplication handler Aug 13, 2025
@hbjORbj hbjORbj marked this pull request as draft August 13, 2025 09:44
@hbjORbj hbjORbj changed the title fix: improve error handling for event type duplication handler fix: more precise error code for event type duplication handler Aug 13, 2025
@graphite-app
Copy link

graphite-app bot commented Aug 13, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (08/13/25)

1 reviewer was added to this PR based on Keith Williams's automation.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🔭 Outside diff range comments (4)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (4)

241-243: Critical: Top-level catch still converts BAD_REQUEST into 500; preserve TRPCErrors and map Prisma P2002 here

As written, any thrown TRPCError (including BAD_REQUEST) will be wrapped into INTERNAL_SERVER_ERROR, reintroducing the false 500. Also, avoid leaking raw error.toString() in responses.

Apply:

-  } catch (error) {
-    throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Error duplicating event type ${error}` });
-  }
+  } catch (error: unknown) {
+    // Preserve explicit TRPC errors
+    if (error instanceof TRPCError) {
+      throw error;
+    }
+    // Map Prisma unique-constraint violations to 400 instead of 500
+    if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
+      const target = (error.meta?.target as string[] | string | undefined);
+      throw new TRPCError({
+        code: "BAD_REQUEST",
+        message: target
+          ? `Unique constraint violation on ${Array.isArray(target) ? target.join(", ") : target}.`
+          : "Unique constraint violation.",
+      });
+    }
+    // Avoid leaking internal error details
+    throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: "Error duplicating event type" });
+  }

31-61: Prisma: replace include with select and drop unused relations

Guideline: For Prisma queries, only select the data you need; never use include. This query pulls many relations (some later discarded as _webhooks, _schedule), increasing payload and risk of leaking fields.

Actionable recommendations:

  • Replace include with select and enumerate only fields actually used below (e.g., id, userId, teamId, bookingFields, rrSegmentQueryValue, assignRRMembersUsingSegment, etc.).
  • For relations:
    • users: select { id: true } (already effectively done, but via include)
    • hashedLink: select { eventTypeId, expiresAt, maxUsageCount }
    • workflows: select { workflowId }
    • team: select { id: true }
    • calVideoSettings: keep your current fine-grained select
  • Remove fetching webhooks and schedule altogether since they’re not used.

Example pattern:

const eventType = await prisma.eventType.findUnique({
  where: { id: originalEventTypeId },
  select: {
    id: true,
    userId: true,
    teamId: true,
    // ...
    users: { select: { id: true } },
    hashedLink: { select: { eventTypeId: true, expiresAt: true, maxUsageCount: true } },
    workflows: { select: { workflowId: true } },
    team: { select: { id: true } },
    calVideoSettings: { select: { /* as you have */ } },
  },
});

200-203: Fix potential runtime crash when users is undefined

users[0] throws if users is undefined. Use optional chaining on the array access.

Apply:

-        link: generateHashedLink(
-          `${users[0]?.id ?? newEventType.teamId ?? originalLink.eventTypeId}-${index}`
-        ),
+        link: generateHashedLink(
+          `${users?.[0]?.id ?? newEventType.teamId ?? originalLink.eventTypeId}-${index}`
+        ),

239-240: Enforce explicit field selection in EventTypeRepository.create and trim returned data in duplicate.handler.ts

The current create() call returns the full DB record (including all columns), which risks unintentionally exposing fields if the schema evolves. Please:

  • In packages/lib/server/repository/eventTypeRepository.ts: replace the default return (with only include) by a select listing each field you intend to expose.
  • In packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts: stop returning the raw newEventType object. Instead, map out a safe DTO containing only the selected properties.

No credential.key field is present in this handler.

Suggested diff for EventTypeRepository.create:

--- a/packages/lib/server/repository/eventTypeRepository.ts
+++ b/packages/lib/server/repository/eventTypeRepository.ts
@@ async create(data: IEventType) {
-    return await this.prismaClient.eventType.create({
-      data: this.generateCreateEventTypeData(data),
-      include: {
-        calVideoSettings: true,
-      },
-    });
+    return await this.prismaClient.eventType.create({
+      data: this.generateCreateEventTypeData(data),
+      select: {
+        id: true,
+        name: true,
+        slug: true,
+        description: true,
+        duration: true,
+        metadata: true,
+        calVideoSettings: true,
+        createdAt: true,
+        updatedAt: true,
+      },
+    });

And in duplicate.handler.ts:

--- a/packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts
+++ b/packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts
@@
-    return {
-      eventType: newEventType,
-    };
+    return {
+      eventType: {
+        id: newEventType.id,
+        name: newEventType.name,
+        slug: newEventType.slug,
+        description: newEventType.description,
+        duration: newEventType.duration,
+        metadata: newEventType.metadata,
+        calVideoSettings: newEventType.calVideoSettings,
+        createdAt: newEventType.createdAt,
+        updatedAt: newEventType.updatedAt,
+      },
+    };
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 94a04f0 and 783f260.

📒 Files selected for processing (1)
  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.ts

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

**/*.ts: For Prisma queries, only select data you need; never use include, always use select
Ensure the credential.key field is never returned from tRPC endpoints or APIs

Files:

  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code; prefer native Date or Day.js .utc() in hot paths like loops

Files:

  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts
🧬 Code Graph Analysis (1)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (1)
packages/platform/libraries/index.ts (1)
  • TRPCError (56-56)

@vercel
Copy link

vercel bot commented Aug 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
cal ⬜️ Ignored Aug 14, 2025 10:33am
cal-eu ⬜️ Ignored Aug 14, 2025 10:33am

@hbjORbj hbjORbj marked this pull request as ready for review August 13, 2025 09:58
@hbjORbj hbjORbj force-pushed the fix/false-500-from-duplication-error branch from 0b6ba83 to 12ced11 Compare August 13, 2025 09:59
@github-actions
Copy link
Contributor

github-actions bot commented Aug 13, 2025

E2E results are ready!

@hbjORbj hbjORbj enabled auto-merge (squash) August 14, 2025 10:32
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts (1)

3-3: Avoid importing from Prisma runtime internals; use Prisma.PrismaClientKnownRequestError

Importing from @prisma/client/runtime/library is brittle as mentioned in previous reviews. The duplicate.handler.ts file already imports Prisma from "@prisma/client", so you should use Prisma.PrismaClientKnownRequestError instead.

-import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
+import { Prisma } from "@prisma/client";

And update the usage:

-            new PrismaClientKnownRequestError("Unique constraint failed", {
+            new Prisma.PrismaClientKnownRequestError("Unique constraint failed", {
🧹 Nitpick comments (1)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts (1)

16-18: Consider adding TypeScript types for test data.

While the as any type assertion works for testing, consider defining proper types for better test maintainability, especially for the ctx object which has a specific structure expected by the handler.

-  const ctx = { user: { id: 1, profile: { id: 1 } } } as any;
+  const ctx = {
+    user: {
+      id: 1,
+      profile: { id: 1 }
+    }
+  } as Parameters<typeof duplicateHandler>[0]['ctx'];
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these settings in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 783f260 and fb08bd9.

📒 Files selected for processing (2)
  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts (1 hunks)
  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.ts

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

**/*.ts: For Prisma queries, only select data you need; never use include, always use select
Ensure the credential.key field is never returned from tRPC endpoints or APIs

Files:

  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts
  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)

Flag excessive Day.js use in performance-critical code; prefer native Date or Day.js .utc() in hot paths like loops

Files:

  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts
  • packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts
🧬 Code Graph Analysis (2)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts (1)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (1)
  • duplicateHandler (21-238)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (2)
apps/api/v2/src/lib/logger.bridge.ts (1)
  • error (77-79)
packages/platform/libraries/index.ts (1)
  • TRPCError (56-56)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Install dependencies / Yarn install & cache
🔇 Additional comments (2)
packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.ts (1)

229-235: LGTM! Correct error mapping for unique constraint violations.

The implementation properly maps Prisma's unique constraint error (P2002) to a BAD_REQUEST response instead of the generic 500 error, which aligns with the PR objective to provide more precise error codes.

packages/trpc/server/routers/viewer/eventTypes/duplicate.handler.test.ts (1)

25-45: Comprehensive test coverage for the error mapping logic.

The test effectively validates that P2002 errors are correctly mapped to BAD_REQUEST responses. The test structure is well-organized with proper mocking and assertion patterns.

Copy link
Contributor

@volnei volnei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hbjORbj hbjORbj merged commit c989817 into main Aug 14, 2025
65 of 67 checks passed
@hbjORbj hbjORbj deleted the fix/false-500-from-duplication-error branch August 14, 2025 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐛 bug Something isn't working core area: core, team members only event-types area: event types, event-types foundation ready-for-e2e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants