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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user