refactor: Enhance unique constraint validation across data operations
- Integrated `TableManagementService` to validate unique constraints before insert, update, and upsert actions in various controllers, including `dataflowExecutionController`, `dynamicFormController`, and `tableManagementController`. - Improved error handling in `errorHandler` to provide detailed messages indicating which field has a unique constraint violation. - Updated the `formatPgError` utility to extract and display specific column labels for unique constraint violations, enhancing user feedback. - Adjusted the table schema retrieval to include company-specific nullable and unique constraints, ensuring accurate representation of database rules. These changes improve data integrity by preventing duplicate entries and enhance user experience through clearer error messages related to unique constraints.
This commit is contained in:
@@ -41,7 +41,18 @@ export const errorHandler = (
|
||||
// PostgreSQL 에러 코드 참조: https://www.postgresql.org/docs/current/errcodes-appendix.html
|
||||
if (pgError.code === "23505") {
|
||||
// unique_violation
|
||||
error = new AppError("중복된 데이터가 존재합니다.", 400);
|
||||
const constraint = pgError.constraint || "";
|
||||
const tbl = pgError.table || "";
|
||||
let col = "";
|
||||
if (constraint && tbl) {
|
||||
const prefix = `${tbl}_`;
|
||||
const suffix = "_key";
|
||||
if (constraint.startsWith(prefix) && constraint.endsWith(suffix)) {
|
||||
col = constraint.slice(prefix.length, -suffix.length);
|
||||
}
|
||||
}
|
||||
const detail = col ? ` [${col}]` : "";
|
||||
error = new AppError(`중복된 데이터가 존재합니다.${detail}`, 400);
|
||||
} else if (pgError.code === "23503") {
|
||||
// foreign_key_violation
|
||||
error = new AppError("참조 무결성 제약 조건 위반입니다.", 400);
|
||||
|
||||
Reference in New Issue
Block a user