30 lines
644 B
TypeScript
30 lines
644 B
TypeScript
import { NextResponse } from "next/server";
|
|
import type { HttpError } from "@/lib/types";
|
|
|
|
export function jsonError(
|
|
message: string,
|
|
statusCode = 500,
|
|
extra: Record<string, unknown> = {},
|
|
) {
|
|
return NextResponse.json(
|
|
{
|
|
ok: false,
|
|
error: message,
|
|
...extra,
|
|
},
|
|
{
|
|
status: statusCode,
|
|
},
|
|
);
|
|
}
|
|
|
|
export function normalizeError(error: unknown, fallbackMessage: string) {
|
|
const normalizedError = error as HttpError | null;
|
|
|
|
return {
|
|
statusCode: normalizedError?.statusCode ?? 500,
|
|
message: normalizedError?.message ?? fallbackMessage,
|
|
detail: normalizedError?.detail ?? null,
|
|
};
|
|
}
|