Initial commit
This commit is contained in:
23
lib/http.ts
Normal file
23
lib/http.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { PublicError, publicMessageFor } from "./errors";
|
||||
|
||||
export function ok(data: any = {}) {
|
||||
return NextResponse.json({ ok: true, ...data });
|
||||
}
|
||||
|
||||
export function fail(err: unknown, opts?: { adminMessage?: string; status?: number; isAdmin?: boolean }) {
|
||||
const status = opts?.status ?? 400;
|
||||
|
||||
if (opts?.isAdmin) {
|
||||
const msg =
|
||||
err instanceof PublicError
|
||||
? err.message
|
||||
: err instanceof Error
|
||||
? err.message
|
||||
: "Unknown error";
|
||||
return NextResponse.json({ ok: false, message: msg }, { status });
|
||||
}
|
||||
|
||||
// consumer-safe
|
||||
return NextResponse.json({ ok: false, message: publicMessageFor("GENERIC") }, { status });
|
||||
}
|
||||
Reference in New Issue
Block a user