refactor: replace @prisma/client/runtime imports with public API (#23087)
- Replace PrismaClientKnownRequestError and other error classes with Prisma namespace equivalents - Remove internal DefaultArgs and InternalArgs type imports from Prisma extensions - Ensure all error handling uses stable public API exports - Maintain compatibility with future Prisma versions by avoiding runtime dependencies Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com>
This commit is contained in:
co-authored by
Devin AI
Keith Williams
parent
5b2b5fd967
commit
6defa1cd7d
@@ -2,13 +2,7 @@ import { extractUserContext } from "@/lib/extract-user-context";
|
||||
import { filterReqHeaders } from "@/lib/filterReqHeaders";
|
||||
import type { ArgumentsHost, ExceptionFilter } from "@nestjs/common";
|
||||
import { Catch, HttpStatus, Logger } from "@nestjs/common";
|
||||
import {
|
||||
PrismaClientInitializationError,
|
||||
PrismaClientKnownRequestError,
|
||||
PrismaClientRustPanicError,
|
||||
PrismaClientUnknownRequestError,
|
||||
PrismaClientValidationError,
|
||||
} from "@prisma/client/runtime/library";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Request } from "express";
|
||||
|
||||
import {
|
||||
@@ -21,18 +15,18 @@ import {
|
||||
import { Response } from "@calcom/platform-types";
|
||||
|
||||
type PrismaError =
|
||||
| PrismaClientInitializationError
|
||||
| PrismaClientKnownRequestError
|
||||
| PrismaClientRustPanicError
|
||||
| PrismaClientUnknownRequestError
|
||||
| PrismaClientValidationError;
|
||||
| Prisma.PrismaClientInitializationError
|
||||
| Prisma.PrismaClientKnownRequestError
|
||||
| Prisma.PrismaClientRustPanicError
|
||||
| Prisma.PrismaClientUnknownRequestError
|
||||
| Prisma.PrismaClientValidationError;
|
||||
|
||||
@Catch(
|
||||
PrismaClientInitializationError,
|
||||
PrismaClientKnownRequestError,
|
||||
PrismaClientRustPanicError,
|
||||
PrismaClientUnknownRequestError,
|
||||
PrismaClientValidationError
|
||||
Prisma.PrismaClientInitializationError,
|
||||
Prisma.PrismaClientKnownRequestError,
|
||||
Prisma.PrismaClientRustPanicError,
|
||||
Prisma.PrismaClientUnknownRequestError,
|
||||
Prisma.PrismaClientValidationError
|
||||
)
|
||||
export class PrismaExceptionFilter implements ExceptionFilter {
|
||||
private readonly logger = new Logger("PrismaExceptionFilter");
|
||||
@@ -58,7 +52,7 @@ export class PrismaExceptionFilter implements ExceptionFilter {
|
||||
let message = "There was an error, please try again later.";
|
||||
let statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
let errorCode = INTERNAL_SERVER_ERROR;
|
||||
if (error instanceof PrismaClientKnownRequestError) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
switch (error.code) {
|
||||
case "P2002": // Unique constraint failed
|
||||
errorCode = CONFLICT;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export function getRequestedSlugError(error: unknown, requestedSlug: string) {
|
||||
let message = `Unknown error`;
|
||||
@@ -6,7 +6,7 @@ export function getRequestedSlugError(error: unknown, requestedSlug: string) {
|
||||
// This covers the edge case if an unpublished team takes too long to publish
|
||||
// and another team gets the requestedSlug first.
|
||||
// https://www.prisma.io/docs/reference/api-reference/error-reference#p2002
|
||||
if (error instanceof PrismaClientKnownRequestError && error.code === "P2002") {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002") {
|
||||
statusCode = 400;
|
||||
message = `It seems like the requestedSlug: '${requestedSlug}' is already taken. Please contact support at help@cal.com so we can resolve this issue.`;
|
||||
} else if (error instanceof Error) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { Account, IdentityProvider, Prisma, User, VerificationToken } from "@prisma/client";
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
|
||||
import type { Account, IdentityProvider, User, VerificationToken } from "@prisma/client";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import type { Awaitable } from "next-auth";
|
||||
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
@@ -86,7 +86,7 @@ export default function CalComAdapter(prismaClient: PrismaClient): CalComAdapter
|
||||
} catch (error) {
|
||||
// If token already used/deleted, just return null
|
||||
// https://www.prisma.io/docs/reference/api-reference/error-reference#p2025
|
||||
if (error instanceof PrismaClientKnownRequestError) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (error.code === "P2025") return null;
|
||||
}
|
||||
throw error;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import type { DefaultArgs, InternalArgs } from "@prisma/client/runtime/library";
|
||||
|
||||
export function excludeLockedUsersExtension() {
|
||||
return Prisma.defineExtension({
|
||||
@@ -35,11 +34,11 @@ function safeJSONStringify(x: any) {
|
||||
|
||||
async function excludeLockedUsers(
|
||||
args:
|
||||
| Prisma.UserFindUniqueArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.UserFindFirstArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.UserFindManyArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.UserFindUniqueOrThrowArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.UserFindFirstOrThrowArgs<InternalArgs & DefaultArgs>,
|
||||
| Prisma.UserFindUniqueArgs
|
||||
| Prisma.UserFindFirstArgs
|
||||
| Prisma.UserFindManyArgs
|
||||
| Prisma.UserFindUniqueOrThrowArgs
|
||||
| Prisma.UserFindFirstOrThrowArgs,
|
||||
query: <T>(args: T) => Promise<unknown>
|
||||
) {
|
||||
args.where = args.where || {};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import type { DefaultArgs, InternalArgs } from "@prisma/client/runtime/library";
|
||||
|
||||
export function excludePendingPaymentsExtension() {
|
||||
return Prisma.defineExtension({
|
||||
@@ -27,11 +26,11 @@ export function excludePendingPaymentsExtension() {
|
||||
|
||||
async function excludePendingPayments(
|
||||
args:
|
||||
| Prisma.TeamFindUniqueArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.TeamFindFirstArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.TeamFindManyArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.TeamFindUniqueOrThrowArgs<InternalArgs & DefaultArgs>
|
||||
| Prisma.TeamFindFirstOrThrowArgs<InternalArgs & DefaultArgs>,
|
||||
| Prisma.TeamFindUniqueArgs
|
||||
| Prisma.TeamFindFirstArgs
|
||||
| Prisma.TeamFindManyArgs
|
||||
| Prisma.TeamFindUniqueOrThrowArgs
|
||||
| Prisma.TeamFindFirstOrThrowArgs,
|
||||
query: <T>(args: T) => Promise<unknown>
|
||||
) {
|
||||
args.where = args.where || {};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
import { DailyLocationType } from "@calcom/app-store/locations";
|
||||
import { getDefaultLocations } from "@calcom/lib/server/getDefaultLocations";
|
||||
@@ -139,7 +138,7 @@ export const createHandler = async ({ ctx, input }: CreateOptions) => {
|
||||
return { eventType };
|
||||
} catch (e) {
|
||||
console.warn(e);
|
||||
if (e instanceof PrismaClientKnownRequestError) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
if (e.code === "P2002" && Array.isArray(e.meta?.target) && e.meta?.target.includes("slug")) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "URL Slug already exists for given user." });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user