From 2c04c97a02f81a619f18ffde3a4ac26987d4f6cc Mon Sep 17 00:00:00 2001 From: yiling Date: Thu, 23 Jul 2026 11:35:18 +0800 Subject: [PATCH] refactor(ai-usage): use Prisma.PrismaClientKnownRequestError for P2002 handling --- packages/core/src/ai-usage/ai-usage.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/core/src/ai-usage/ai-usage.ts b/packages/core/src/ai-usage/ai-usage.ts index a260d64d..9ae29823 100644 --- a/packages/core/src/ai-usage/ai-usage.ts +++ b/packages/core/src/ai-usage/ai-usage.ts @@ -1,4 +1,4 @@ -import { prisma } from '@shumai/db' +import { prisma, Prisma } from '@shumai/db' import type { TeamUsageStatsResponse, Timeframe } from '@shumai/dtos' import { HTTPException } from 'hono/http-exception' @@ -92,13 +92,8 @@ export class AiUsageService { cost: params.cost, }, }) - } catch (err: unknown) { - if ( - err && - typeof err === 'object' && - 'code' in err && - (err as { code: string }).code === 'P2002' - ) { + } catch (err) { + if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') { await prisma.aiUsage.updateMany({ where: { teamId: params.teamId,