Files

31 lines
817 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import "server-only";
import { NextResponse } from "next/server";
import { DomainError } from "../domain/errors";
export function apiSuccess<T>(data: T, init?: ResponseInit): NextResponse {
return NextResponse.json({ ok: true, data }, init);
}
export function apiError(error: unknown): NextResponse {
if (error instanceof DomainError) {
return NextResponse.json(
{
ok: false,
error: {
code: error.code,
message: error.message,
...(error.details ? { details: error.details } : {}),
},
},
{ status: error.status },
);
}
console.error("Unhandled API error", error);
return NextResponse.json(
{ ok: false, error: { code: "INTERNAL_ERROR", message: "Beklenmeyen bir sunucu hatası oluştu." } },
{ status: 500 },
);
}