fix(library): 文章库导入失败时透传结构化 detail,恢复粘贴模式降级提示#62
Merged
Conversation
# 现象
/library 里粘贴反爬虫/SPA/无效 URL 导入时,后端正确返回
`{"detail":{"error":"fetch_failed","hint":"抓取失败,请改用粘贴正文模式"}}`,
但用户屏幕上看到的是一坨 JSON 串,且不会自动切到粘贴正文模式。
# 根因
authFetchJson 拿到对象型 detail 后 JSON.stringify 包进 new Error(detail)
抛出,调用方拿到的 err 只有 message(字符串),没有 .detail(对象)。
Library.vue 的友好降级分支 `typeof err.detail === 'object'` 永远 false,
fetchFailHint / 自动切 markdown 模式 / "该页面无法自动抓取" 全部走不到。
# 修复
authFetchJson 在 raw 是对象时:
- message 取 raw.hint || raw.error || JSON.stringify(raw)(仍向后兼容)
- 把原对象挂到 err.detail,让调用方按业务字段降级
仅 Library.vue 一处用 .detail;其余 130+ 调用方只看 .message,行为不变。
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
现象
访问
/library,用 URL 模式导入大部分非白名单站点(知乎、Twitter、付费墙、反爬虫等)时:{"error":"fetch_failed","hint":"抓取失败,请改用粘贴正文模式"}根因
两条独立链路叠加:
app/services/library_service.py抓取失败时返回400 + {detail:{error:"fetch_failed", hint:"..."}},本意是让前端把用户引导到粘贴模式useAuthFetch.js拿到对象型detail后JSON.stringify包进new Error()抛出。Library.vue 的友好分支检查typeof err.detail === 'object',但Error没有.detail属性 → 永远走不到友好分支修复
useAuthFetch.authFetchJson在 raw 是对象时:err.message取raw.hint || raw.error || JSON.stringify(raw)(更人话)err.detail = raw(透传给调用方按业务字段降级)影响面
Library.vue一处用err.detail,立即拿到正确行为authFetchJson调用方只读.message,从原先的 JSON 串改成hint || error,可读性提升、不破坏任何现有流程验证
useAuthFetch.spec.js9 个用例全绿(含新增 fetch_failed 用例 + 原有对象 detail 用例改写)npm run build通过uvicorn main:app端到端跑:POST /library/articles {url:"https://example.com/nonexistent..."}→ 400 fetch_faileduseAuthFetch链路 →err.detail.error === 'fetch_failed'→ 走友好分支 → 自动切 markdown 模式 + 显示 hint不在范围
/bbc-eaw/episodes → /articles/episodes重命名遗留失败(main 上已经红,不归本 PR)Test plan
/library用一个明知抓不到的 URL(如https://zhuanlan.zhihu.com/p/xxx),确认:🤖 Generated with Claude Code