/** * 读取错误响应体文本(最多 4096 字符),用于拼入错误信息。 * 若读取失败则返回兜底字符串。 */ export async function readErrorBody(res: Response): Promise { try { const text = await res.text(); return text.slice(0, 4096); } catch { return "(unable to read response body)"; } } /** 将 FormData 内容转为可读摘要字符串,用于拼入错误信息。 */ export function summarizeFormData(form: FormData): string { const parts: string[] = []; for (const [key, value] of form.entries()) { if (typeof value === "string") { parts.push(`${key}: ${value.slice(0, 2048)}`); } else { parts.push( `${key}: `, ); } } return parts.length > 0 ? parts.join("\n") : "(empty body)"; }