Skip to content

fix(twitter): 按 HTTP 状态翻译 API 错误 + 修 SearchTimeline queryId 误取#1890

Merged
jackwener merged 2 commits into
jackwener:mainfrom
huanghe:pr/twitter-api-error-hardening
Jun 11, 2026
Merged

fix(twitter): 按 HTTP 状态翻译 API 错误 + 修 SearchTimeline queryId 误取#1890
jackwener merged 2 commits into
jackwener:mainfrom
huanghe:pr/twitter-api-error-hardening

Conversation

@huanghe

@huanghe huanghe commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

本 PR 与 #1891(media_posters)同从原 #1875 拆分而来,按单一关注点分成「健壮性」与「字段增强」两个独立 PR。本 PR 只含健壮性,不引入任何新输出字段。

集中提升 Twitter 读命令在 API 失败时的可诊断性与 queryId 解析正确性。改动主要落在 clis/twitter/shared.js(被各读命令共享)。

一、describeTwitterApiError():按 HTTP 状态翻译 API 失败

各读命令的 API 失败原来统一抛 "queryId may have expired",掩盖真实原因。新增 describeTwitterApiError(operation, status) 按状态码翻译:

状态 翻译
429 会话配额限流,建议冷却 15-30 分钟后重试
401 cookie 失效,需重新登录
403 权限不足 / 资源私密
404 资源不存在(删除 / 封禁 / 私密)
5xx 服务端瞬时错误,可重试
其他 queryId 过期 / schema 变更 / 瞬时

调用方据此能区分限流 / 鉴权 / schema 变更 / 瞬时错误,采取重试 / 冷却 / 重新登录 / 丢弃等不同处理。覆盖 timeline / search / thread / tweets / likes / list-tweets / bookmarks / following / lists / profile / article 等读命令。

二、parseOperationFromBundleText() + queryId 解析修复

resolveTwitterOperationMetadata() 改用「operationName 锚定」正则解析 SearchTimeline 的 queryId,并采用 network / GitHub-first 回退顺序。修复此前在多 operation 的 bundle 文本里取到错误 queryId 的问题——旧逻辑按 bundle 优先,会静默返回相邻 operation 的 id,导致请求被 Twitter 拒绝、又被误报为 "queryId expired"。

测试

  • parseOperationFromBundleText 8 个用例:queryId 正序 / 反序、跨模块污染防护、operationName 缺失、跨对象距离过远、featureSwitches 回退、regex 元字符转义、空输入
  • describeTwitterApiError 各状态码翻译
  • twitter 全量:35 文件 / 411 用例通过;tsc --noEmit 干净;npm run build 干净

兼容性

纯错误信息 / queryId 解析改进,不改任何输出字段语义。

集中提升 Twitter 读命令在 API 失败时的可诊断性与 queryId 解析正确性。改动主要落在 `clis/twitter/shared.js`(被各读命令共享)。

## describeTwitterApiError():按 HTTP 状态翻译 API 失败

各读命令的 API 失败原来统一抛 "queryId may have expired",掩盖真实原因。新增 `describeTwitterApiError(operation, status)` 按状态码翻译:

- 429 → 会话配额限流,建议冷却 15-30 分钟后重试
- 401 → cookie 失效,需重新登录
- 403 → 权限不足 / 资源私密
- 404 → 资源不存在(删除 / 封禁 / 私密)
- 5xx → 服务端瞬时错误,可重试
- 其他 → queryId 过期 / schema 变更 / 瞬时错误

调用方据此能区分限流 / 鉴权 / schema 变更 / 瞬时错误,采取重试 / 冷却 / 重新登录 / 丢弃等不同处理。覆盖 timeline / search / thread / tweets / likes / list-tweets / bookmarks / following / lists / profile / article 等读命令。

## parseOperationFromBundleText() + queryId 解析修复

`resolveTwitterOperationMetadata()` 改用「operationName 锚定」正则解析 SearchTimeline 的 queryId,并采用 network / GitHub-first 回退顺序。修复此前在多 operation 的 bundle 文本里取到**错误 queryId** 的问题——旧逻辑按 bundle 优先,会静默返回相邻 operation 的 id,导致请求被 Twitter 拒绝、误报为 "queryId expired"。

## 测试

- `parseOperationFromBundleText` 8 个用例:queryId 正序 / 反序、跨模块污染防护、operationName 缺失、跨对象距离过远、featureSwitches 回退、regex 元字符转义、空输入
- `describeTwitterApiError` 各状态码翻译
- twitter 全量:35 文件 / 411 用例通过;`tsc --noEmit` 干净;`npm run build` 干净

## 兼容性

纯错误信息 / queryId 解析改进,不改任何输出字段语义。
@jackwener jackwener merged commit 531ef27 into jackwener:main Jun 11, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants