mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 06:17:40 +00:00
feat(core): wrap audit log creation in try catch to prevent blocking APIs
This commit is contained in:
@@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user