Files
Jyotisha/frontend/src/lib/admin/admin-error-response.ts
T
jesse-uxandCursor 0a5d93ab75
Independent Staging Quality Gate / validate (push) Successful in 14m0s
Independent Staging Quality Gate / publish (push) Successful in 4m5s
fix(admin): save published models as a new draft and allow deleting them
Editing a published model tried to update a non-draft row, so the save collapsed into a generic constraint error.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-20 15:02:42 +08:00

67 lines
3.1 KiB
TypeScript

import { NextResponse } from "next/server";
import { AdminAuthorizationError } from "./auth-boundary";
function isPostgresError(error: unknown): error is { code: string } {
return typeof error === "object" && error !== null && "code" in error
&& typeof (error as { code?: unknown }).code === "string";
}
function errorMessage(error: unknown) {
return typeof error === "object" && error !== null && "message" in error
&& typeof (error as { message?: unknown }).message === "string"
? (error as { message: string }).message
: "";
}
export function adminErrorResponse(error: unknown) {
if (error instanceof AdminAuthorizationError) {
return NextResponse.json({ error: error.message }, { status: error.status });
}
const message = errorMessage(error);
if (message.includes("模型供应商地址不在服务器允许列表中")) {
return NextResponse.json({ error: message }, { status: 400 });
}
if (message.includes("网关地址不允许")) {
return NextResponse.json({ error: message }, { status: 400 });
}
if (message.includes("model_provider_url_unsafe")) {
return NextResponse.json({ error: "模型供应商地址不安全或解析到内网" }, { status: 400 });
}
if (message.includes("模型供应商密钥未配置") || message.includes("model_provider_key_required")) {
return NextResponse.json({ error: "模型供应商密钥未配置" }, { status: 409 });
}
if (message.includes("product_not_found")) {
return NextResponse.json({ error: "商品不存在" }, { status: 404 });
}
if (message.includes("product_code_immutable")) {
return NextResponse.json({ error: "商品代码不可修改" }, { status: 400 });
}
if (message.includes("product_draft_not_found")) {
return NextResponse.json({ error: "只能编辑草稿。已发布商品请下架,或保存为新版本草稿后再发布" }, { status: 400 });
}
if (message.includes("model_version_not_found")) {
return NextResponse.json({ error: "模型版本不存在" }, { status: 404 });
}
if (message.includes("model_draft_not_found")) {
return NextResponse.json({ error: "只能修改草稿。已发布模型保存后会生成新版本,不会直接改当前版本" }, { status: 400 });
}
if (message.includes("model_default_required")) {
return NextResponse.json({ error: "请先把另一个已发布模型设为默认,再删除这个模型" }, { status: 409 });
}
if (isPostgresError(error)) {
if (error.code === "42501") return NextResponse.json({ error: "无权执行此操作" }, { status: 403 });
if (error.code === "40001") return NextResponse.json({ error: "资源已被其他管理员修改,请刷新后重试" }, { status: 409 });
if (error.code === "23503") {
return NextResponse.json({ error: "已有订单或订阅引用该商品,不能硬删除,请改为下架" }, { status: 409 });
}
if (error.code === "22023" || error.code === "23514" || error.code === "23505") {
return NextResponse.json({ error: "提交内容不符合业务约束" }, { status: 400 });
}
}
return NextResponse.json(
{ error: "后台服务暂时不可用" },
{ status: 500 },
);
}