From fb66166b4975ba16cfee04db45796beb2b7755ca Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 1 Aug 2026 15:50:35 +0800 Subject: [PATCH] feat(core): wrap audit log creation in try catch to prevent blocking APIs --- packages/core/src/auditLog/auditLog.test.ts | 9 ++++++++ packages/core/src/auditLog/auditLog.ts | 24 +++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/core/src/auditLog/auditLog.test.ts b/packages/core/src/auditLog/auditLog.test.ts index 28e8e277..ababe7ed 100644 --- a/packages/core/src/auditLog/auditLog.test.ts +++ b/packages/core/src/auditLog/auditLog.test.ts @@ -75,4 +75,13 @@ describe('AuditLogService', () => { expect(resultItem.total).toBe(2) }) + + it('handles database errors gracefully and logs error without throwing', async () => { + const result = await auditLogService.logAction({ + action: AuditAction.project_create, + teamId: 'invalid-non-existent-team-id', + }) + + expect(result).toBeNull() + }) }) diff --git a/packages/core/src/auditLog/auditLog.ts b/packages/core/src/auditLog/auditLog.ts index e8bda0c9..77744a09 100644 --- a/packages/core/src/auditLog/auditLog.ts +++ b/packages/core/src/auditLog/auditLog.ts @@ -2,6 +2,7 @@ import { prisma } from '@shumai/db' import type { Prisma } from '@shumai/db' import type { AuditAction } from '@shumai/dtos' import { paginateQuery } from '@shumai/core/src/pagination' +import { logger } from '@shumai/core/src/logger' export interface LogActionParams { action: AuditAction @@ -22,15 +23,20 @@ export interface ListAuditLogsParams { export class AuditLogService { async logAction(params: LogActionParams) { - return await prisma.auditLog.create({ - data: { - action: params.action, - teamId: params.teamId, - userId: params.userId, - projectId: params.projectId, - itemId: params.itemId, - }, - }) + try { + return await prisma.auditLog.create({ + data: { + action: params.action, + teamId: params.teamId, + userId: params.userId, + projectId: params.projectId, + itemId: params.itemId, + }, + }) + } catch (err) { + logger.error({ err, params }, 'Failed to create audit log') + return null + } } async listAuditLogs(params: ListAuditLogsParams) {