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 에러 처리