datawhalechina/hello-agents · error · Error
大纲生成失败:
Error message
大纲生成失败:
What it means
'大纲生成失败: ' + e (outline generation failed) is alerted by the NovelGenerator frontend when POST {API_URL}/outline/generate responds non-OK; the thrown Error wraps the raw response text (await res.text()). Note the string-concatenation of an Error object yields 'Error: <server text>', so the alert shows the server's body — commonly an HTML error page or FastAPI detail JSON, which is ugly but informative.
Source
Thrown at Co-creation-projects/lgs-only-NovelGenerator/frontend/index.html:383
// 构建 style_tags
const styleTags = {
'channel': outlineInput.channel,
'style': outlineInput.style
};
const res = await fetch(`${API_URL}/outline/generate`, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
novel_id: project.novel_id,
title: project.title,
user_input: outlineInput.user_input,
target_length: outlineInput.target_length,
style_tags: styleTags
})
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
outline.id = data.note_id;
outline.content = data.content;
} catch (e) {
alert('大纲生成失败: ' + e);
} finally {
loading.value = false;
currentAction.value = null;
}
};
const updateOutline = async () => {
if (!outline.id) return;
loading.value = true;
currentAction.value = 'update_outline';
try {
const res = await fetch(`${API_URL}/outline/update`, {View on GitHub (pinned to 606a07d341)
Solutions
- Read the alert content after the colon — it is the raw server response body and names the actual error (e.g. FastAPI {'detail': ...}).
- Verify API_URL matches the running backend and that /outline/generate exists there.
- If the detail mentions a missing novel/project, re-create or re-select the project so novel_id is current.
- Check target_length/style_tags types against the backend schema before sending.
Example fix
// before
if (!res.ok) throw new Error(await res.text());
// ...
alert('大纲生成失败: ' + e);
// after
if (!res.ok) {
const t = await res.text();
throw new Error(`${res.status}: ${t.slice(0, 200)}`);
}
// ...
alert('大纲生成失败: ' + (e instanceof Error ? e.message : String(e))); Defensive patterns
Strategy: validation
Validate before calling
if (!project.novel_id) { alert('请先创建或加载项目'); return; }
if (!outlineInput.user_input?.trim()) { alert('请填写大纲需求描述'); return; }
if (!Number.isFinite(Number(outlineInput.target_length))) { alert('目标字数必须是数字'); return; } Type guard
function isOutlineGenerateResponse(d: unknown): d is { note_id: string; content: string } {
return typeof d === 'object' && d !== null
&& typeof (d as { note_id?: unknown }).note_id === 'string'
&& typeof (d as { content?: unknown }).content === 'string';
} Try / catch
try {
const res = await fetch(`${API_URL}/outline/generate`, opts);
if (!res.ok) {
const detail = await res.text();
throw new Error(`${res.status}: ${detail.slice(0, 200)}`);
}
const data = await res.json();
if (!isOutlineGenerateResponse(data)) throw new Error('大纲响应格式异常');
} catch (e) {
alert('大纲生成失败: ' + (e instanceof Error ? e.message : String(e)));
} Prevention
- Validate project.novel_id and numeric fields before sending — most 4xx here are preventable.
- Show e.message, not the concatenated Error object, in alerts.
- Confirm API_URL is set for the environment (dev vs prod) before release.
When it happens
Trigger: POST /outline/generate with {novel_id, title, user_input, target_length, style_tags} returns 404 (API_URL wrong / route missing), 422 (target_length not a number, empty user_input, style_tags not a list), 500 (LLM generation failure, invalid API key), 502 backend down. Note novel_id comes from project state — a stale/unsaved project id also triggers backend 404s.
Common situations: API_URL constant pointing at the wrong host/port; project created in a previous backend run (memory store reset) so novel_id is unknown; LLM key missing server-side; target_length sent as string from a form input.
Related errors
- 计划模式请求失败: ${resp.status}
- Failed to start session: ${response.statusText}
- Failed to submit sentence: ${response.statusText}
- 保存失败:
- 删除失败:
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/f3a09b6fdbc40267.
Report an issue: GitHub.