fix(api): use PrismaClientKnownRequestError instanceof, add ConflictException for duplicate routes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 01:13:05 +05:30
parent 6b4920ce41
commit d92476f841
3 changed files with 32 additions and 10 deletions
@@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { BadRequestException, ConflictException, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { RoutesService } from './routes.service';
import { PrismaService } from '../../prisma/prisma.service';
@@ -77,6 +78,15 @@ describe('RoutesService', () => {
it('throws BadRequestException when targetGroupId is empty', async () => {
await expect(service.create('grp_1', '')).rejects.toThrow(BadRequestException);
});
it('throws ConflictException when route already exists (Prisma P2002)', async () => {
const p2002 = new Prisma.PrismaClientKnownRequestError('Unique constraint', {
code: 'P2002',
clientVersion: '6.0.0',
});
mockPrisma.syncRoute.create.mockRejectedValueOnce(p2002);
await expect(service.create('grp_1', 'grp_2')).rejects.toThrow(ConflictException);
});
});
describe('remove', () => {
@@ -86,7 +96,11 @@ describe('RoutesService', () => {
});
it('throws NotFoundException when route does not exist (Prisma P2025)', async () => {
mockPrisma.syncRoute.delete.mockRejectedValueOnce({ code: 'P2025' });
const p2025 = new Prisma.PrismaClientKnownRequestError('Record not found', {
code: 'P2025',
clientVersion: '6.0.0',
});
mockPrisma.syncRoute.delete.mockRejectedValueOnce(p2025);
await expect(service.remove('bad_id')).rejects.toThrow(NotFoundException);
});
});