feat(core): wrap audit log creation in try catch to prevent blocking APIs

This commit is contained in:
yiling
2026-08-01 15:50:35 +08:00
parent b0d8ecb589
commit fb66166b49
2 changed files with 24 additions and 9 deletions
@@ -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()
})
})
+15 -9
View File
@@ -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) {