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
+14 -6
View File
@@ -1,4 +1,5 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../../prisma/prisma.service';
const routeInclude = {
@@ -22,17 +23,24 @@ export class RoutesService {
if (!sourceGroupId || !targetGroupId) {
throw new BadRequestException('sourceGroupId and targetGroupId are required');
}
return this.prisma.syncRoute.create({
data: { sourceGroupId, targetGroupId },
include: routeInclude,
});
try {
return await this.prisma.syncRoute.create({
data: { sourceGroupId, targetGroupId },
include: routeInclude,
});
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2002') {
throw new ConflictException('A route between these groups already exists');
}
throw e;
}
}
async remove(id: string) {
try {
await this.prisma.syncRoute.delete({ where: { id } });
} catch (e) {
if ((e as { code?: string })?.code === 'P2025') {
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2025') {
throw new NotFoundException(`Route ${id} not found`);
}
throw e;