22 lines
596 B
TypeScript
22 lines
596 B
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
export function ok(data: unknown, status = 200) {
|
|
return NextResponse.json(data, { status });
|
|
}
|
|
|
|
export function fail(message: string, status = 400, details?: unknown) {
|
|
return NextResponse.json({ message, details }, { status });
|
|
}
|
|
|
|
export function handleAuthError(error: unknown) {
|
|
if (error instanceof Error) {
|
|
if (error.message === "UNAUTHORIZED") {
|
|
return fail("Nicht angemeldet", 401);
|
|
}
|
|
if (error.message === "FORBIDDEN") {
|
|
return fail("Keine Berechtigung", 403);
|
|
}
|
|
}
|
|
return fail("Interner Fehler", 500);
|
|
}
|