From a0ce42e375a0a2d250587040a13751b3dadf87df Mon Sep 17 00:00:00 2001 From: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Date: Tue, 17 Dec 2024 10:40:19 +0200 Subject: [PATCH] chore: improve prisma exception filter messages (#18227) * chore: improve prisma exception filter messages * fixup! chore: improve prisma exception filter messages --- .../v2/src/filters/prisma-exception.filter.ts | 38 +++++++++++++++++-- packages/platform/constants/api.ts | 1 + 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/apps/api/v2/src/filters/prisma-exception.filter.ts b/apps/api/v2/src/filters/prisma-exception.filter.ts index 9bd1f82d10b23a..7977f84961ad07 100644 --- a/apps/api/v2/src/filters/prisma-exception.filter.ts +++ b/apps/api/v2/src/filters/prisma-exception.filter.ts @@ -9,7 +9,13 @@ import { } from "@prisma/client/runtime/library"; import { Request } from "express"; -import { ERROR_STATUS, INTERNAL_SERVER_ERROR } from "@calcom/platform-constants"; +import { + BAD_REQUEST, + CONFLICT, + ERROR_STATUS, + INTERNAL_SERVER_ERROR, + NOT_FOUND, +} from "@calcom/platform-constants"; import { Response } from "@calcom/platform-types"; type PrismaError = @@ -43,11 +49,37 @@ export class PrismaExceptionFilter implements ExceptionFilter { method: request.method, requestId, }); - response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ + + 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) { + switch (error.code) { + case "P2002": // Unique constraint failed + errorCode = CONFLICT; + statusCode = HttpStatus.CONFLICT; + message = "Invalid Input: Trying to create a record that already exists."; + break; + case "P2025": // Record not found + errorCode = NOT_FOUND; + statusCode = HttpStatus.NOT_FOUND; + message = "Invalid Query: The requested record was not found."; + break; + case "P2003": // Foreign key constraint failed + errorCode = BAD_REQUEST; + statusCode = HttpStatus.BAD_REQUEST; + message = "Invalid input: The referenced data does not exist."; + break; + default: + break; + } + } + + response.status(statusCode).json({ status: ERROR_STATUS, timestamp: new Date().toISOString(), path: request.url, - error: { code: INTERNAL_SERVER_ERROR, message: "There was an error, please try again later." }, + error: { code: errorCode, message: message }, }); } } diff --git a/packages/platform/constants/api.ts b/packages/platform/constants/api.ts index 2ebbdebdd5f11e..7ad01d53871365 100644 --- a/packages/platform/constants/api.ts +++ b/packages/platform/constants/api.ts @@ -18,6 +18,7 @@ export const METHOD_NOT_ALLOWED = "METHOD_NOT_ALLOWED"; export const UNPROCESSABLE_ENTITY = "UNPROCESSABLE_ENTITY"; export const ACCESS_TOKEN_EXPIRED = "ACCESS_TOKEN_IS_EXPIRED"; export const INVALID_ACCESS_TOKEN = "Invalid Access Token."; +export const CONFLICT = "CONFLICT"; // Server Errors (5xx) export const INTERNAL_SERVER_ERROR = "INTERNAL_SERVER_ERROR";