feat(project): init
This commit is contained in:
29
lib/http.ts
Normal file
29
lib/http.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user