Curators API: aussagekräftige Fehler bei doppelten Usernames (P2002)

This commit is contained in:
Hördle Bot
2025-12-03 13:37:59 +01:00
parent d7ec691469
commit 41336e3af3

View File

@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client'; import { PrismaClient, Prisma } from '@prisma/client';
import bcrypt from 'bcryptjs'; import bcrypt from 'bcryptjs';
import { requireAdminAuth } from '@/lib/auth'; import { requireAdminAuth } from '@/lib/auth';
@@ -69,6 +69,15 @@ export async function POST(request: NextRequest) {
}); });
} catch (error) { } catch (error) {
console.error('Error creating curator:', error); console.error('Error creating curator:', error);
// Handle unique username constraint violation explicitly
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
return NextResponse.json(
{ error: 'A curator with this username already exists.' },
{ status: 409 }
);
}
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
} }
} }
@@ -153,6 +162,15 @@ export async function PUT(request: NextRequest) {
}); });
} catch (error) { } catch (error) {
console.error('Error updating curator:', error); console.error('Error updating curator:', error);
// Handle unique username constraint violation explicitly for updates
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
return NextResponse.json(
{ error: 'A curator with this username already exists.' },
{ status: 409 }
);
}
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
} }
} }