From bca04f2881e0b32e32ad3f5e35f970d345c5fe59 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Wed, 20 May 2026 20:23:41 +0700 Subject: [PATCH] fixed Promise.all Without Error Handling --- src/controllers/AuthRoleController.ts | 28 ++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/controllers/AuthRoleController.ts b/src/controllers/AuthRoleController.ts index 2e51a79d..7cbc387d 100644 --- a/src/controllers/AuthRoleController.ts +++ b/src/controllers/AuthRoleController.ts @@ -267,11 +267,29 @@ export class AuthRoleController extends Controller { return newAttr; }); const before = structuredClone(record); - await Promise.all([ - this.authRoleRepo.save(record, { data: req }), - setLogDataDiff(req, { before, after: record }), - ...newAttrs.map((attr) => this.authRoleAttrRepo.save(attr)), - ]); + + const queryRunner = AppDataSource.createQueryRunner(); + await queryRunner.connect(); + await queryRunner.startTransaction(); + + try { + await queryRunner.manager.save(AuthRole, record); + await Promise.all( + newAttrs.map((attr) => queryRunner.manager.save(AuthRoleAttr, attr)) + ); + await queryRunner.commitTransaction(); + + setLogDataDiff(req, { before, after: record }); + } catch (error) { + await queryRunner.rollbackTransaction(); + console.error("Error saving auth role:", error); + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาดในการบันทึกข้อมูลบทบาท กรุณาลองใหม่ในภายหลัง" + ); + } finally { + await queryRunner.release(); + } let redisClient; try {