From ec5fae1a4d9d1fb8e791a66cd8bb8eb510f85bb0 Mon Sep 17 00:00:00 2001 From: kjs Date: Wed, 1 Oct 2025 14:57:25 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Prisma=20=EC=97=90=EB=9F=AC=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=BD=94=EB=93=9C=EB=A5=BC=20PostgreSQL=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=BD=94=EB=93=9C=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 변경사항: 1. errorHandler.ts: - Prisma 에러 처리 제거 - PostgreSQL 에러 코드 기반 처리 추가: * 23505: unique_violation (중복 데이터) * 23503: foreign_key_violation (참조 무결성) * 23502: not_null_violation (필수값 누락) 2. dataflowDiagramController.ts: - P2002 (Prisma) → 23505 (PostgreSQL) - unique constraint 에러 처리 개선 3. commonCodeController.ts: - Prisma 에러 처리 주석 수정 - PostgreSQL 23505 에러 코드 추가 최종 확인: - ✅ prisma. 호출: 0개 - ✅ PrismaClient import: 0개 - ✅ Prisma 파일: 0개 - ✅ package.json Prisma 의존성: 0개 - ✅ TypeScript 컴파일 에러: 0개 - ✅ 모든 Prisma 관련 코드 제거 완료 --- .../src/controllers/commonCodeController.ts | 6 ++-- .../controllers/dataflowDiagramController.ts | 4 +-- backend-node/src/middleware/errorHandler.ts | 29 ++++++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/backend-node/src/controllers/commonCodeController.ts b/backend-node/src/controllers/commonCodeController.ts index b75160fc..840983a8 100644 --- a/backend-node/src/controllers/commonCodeController.ts +++ b/backend-node/src/controllers/commonCodeController.ts @@ -133,10 +133,10 @@ export class CommonCodeController { } catch (error) { logger.error("카테고리 생성 실패:", error); - // Prisma 에러 처리 + // PostgreSQL 에러 처리 if ( - error instanceof Error && - error.message.includes("Unique constraint") + ((error as any)?.code === "23505") || // PostgreSQL unique_violation + (error instanceof Error && error.message.includes("Unique constraint")) ) { return res.status(409).json({ success: false, diff --git a/backend-node/src/controllers/dataflowDiagramController.ts b/backend-node/src/controllers/dataflowDiagramController.ts index e18ef615..ad64db21 100644 --- a/backend-node/src/controllers/dataflowDiagramController.ts +++ b/backend-node/src/controllers/dataflowDiagramController.ts @@ -154,7 +154,7 @@ export const createDataflowDiagram = async (req: Request, res: Response) => { // 중복 이름 에러인지 먼저 확인 (로그 출력 전에) const isDuplicateError = - (error && typeof error === "object" && (error as any).code === "P2002") || // Prisma unique constraint error code + (error && typeof error === "object" && (error as any).code === "23505") || // PostgreSQL unique_violation (error instanceof Error && (error.message.includes("unique constraint") || error.message.includes("Unique constraint") || @@ -236,7 +236,7 @@ export const updateDataflowDiagram = async (req: Request, res: Response) => { } catch (error) { // 중복 이름 에러인지 먼저 확인 (로그 출력 전에) const isDuplicateError = - (error && typeof error === "object" && (error as any).code === "P2002") || // Prisma unique constraint error code + (error && typeof error === "object" && (error as any).code === "23505") || // PostgreSQL unique_violation (error instanceof Error && (error.message.includes("unique constraint") || error.message.includes("Unique constraint") || diff --git a/backend-node/src/middleware/errorHandler.ts b/backend-node/src/middleware/errorHandler.ts index 6ffbf83c..611e5d08 100644 --- a/backend-node/src/middleware/errorHandler.ts +++ b/backend-node/src/middleware/errorHandler.ts @@ -25,16 +25,25 @@ export const errorHandler = ( let error = { ...err }; error.message = err.message; - // Prisma 에러 처리 - if (err.name === "PrismaClientKnownRequestError") { - const message = "데이터베이스 요청 오류가 발생했습니다."; - error = new AppError(message, 400); - } - - // Prisma 유효성 검증 에러 - if (err.name === "PrismaClientValidationError") { - const message = "입력 데이터가 유효하지 않습니다."; - error = new AppError(message, 400); + // PostgreSQL 에러 처리 (pg 라이브러리) + if ((err as any).code) { + const pgError = err as any; + // PostgreSQL 에러 코드 참조: https://www.postgresql.org/docs/current/errcodes-appendix.html + if (pgError.code === "23505") { + // unique_violation + error = new AppError("중복된 데이터가 존재합니다.", 400); + } else if (pgError.code === "23503") { + // foreign_key_violation + error = new AppError("참조 무결성 제약 조건 위반입니다.", 400); + } else if (pgError.code === "23502") { + // not_null_violation + error = new AppError("필수 입력값이 누락되었습니다.", 400); + } else if (pgError.code.startsWith("23")) { + // 기타 무결성 제약 조건 위반 + error = new AppError("데이터 무결성 제약 조건 위반입니다.", 400); + } else { + error = new AppError("데이터베이스 오류가 발생했습니다.", 500); + } } // JWT 에러 처리