46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
export const OPENCLAW_UPGRADE_RETRY_MESSAGE =
|
|
"claw正在升级更新稍等5分钟再试";
|
|
export const OPENCLAW_AUTH_REQUIRED_CODE = "AUTH_REQUIRED";
|
|
export const OPENCLAW_AUTH_REQUIRED_MESSAGE =
|
|
"登录状态已失效,请重新登录后使用 Claw";
|
|
export const OPENCLAW_FORBIDDEN_MESSAGE = "当前账号无权访问该 Claw 对话";
|
|
|
|
function readPayloadString(payload: unknown, key: "code" | "error") {
|
|
if (!payload || typeof payload !== "object" || !(key in payload)) {
|
|
return "";
|
|
}
|
|
|
|
const value = (payload as Record<string, unknown>)[key];
|
|
return typeof value === "string" ? value.trim() : "";
|
|
}
|
|
|
|
export function getOpenClawClientErrorMessage(
|
|
status: number,
|
|
payload: unknown
|
|
) {
|
|
const error = readPayloadString(payload, "error");
|
|
|
|
if (isOpenClawAuthRequiredError(status, payload)) {
|
|
return OPENCLAW_AUTH_REQUIRED_MESSAGE;
|
|
}
|
|
if (status === 403) {
|
|
return OPENCLAW_FORBIDDEN_MESSAGE;
|
|
}
|
|
if (status >= 400 && status < 500 && status !== 401 && error) {
|
|
return error;
|
|
}
|
|
return OPENCLAW_UPGRADE_RETRY_MESSAGE;
|
|
}
|
|
|
|
export function isOpenClawAuthRequiredError(
|
|
status: number,
|
|
payload: unknown
|
|
) {
|
|
const code = readPayloadString(payload, "code");
|
|
const error = readPayloadString(payload, "error");
|
|
return (
|
|
status === 401 &&
|
|
(code === OPENCLAW_AUTH_REQUIRED_CODE || /^Unauthorized$/i.test(error))
|
|
);
|
|
}
|