17 lines
625 B
TypeScript
17 lines
625 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { AuthError } from "./session";
|
|
|
|
export function authErrorResponse(error: unknown) {
|
|
if (error instanceof AuthError) {
|
|
return NextResponse.json({ error: error.message }, { status: error.status });
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function apiRouteErrorResponse(error: unknown, logLabel?: string) {
|
|
const authRes = authErrorResponse(error);
|
|
if (authRes) return authRes;
|
|
if (logLabel) console.error(`[${logLabel}]`, error);
|
|
const message = error instanceof Error ? error.message : "伺服器錯誤";
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
} |