Skip to content

fix(gemini): 删除 generate_content 中位于 except Exception 之后的不可达重复异常处理块 - #2039

Open
Anai-Guo wants to merge 1 commit into
Mai-with-u:mainfrom
Anai-Guo:fix/gemini-unreachable-except
Open

Anai-Guo wants to merge 1 commit into
Mai-with-u:mainfrom
Anai-Guo:fix/gemini-unreachable-except

Conversation

@Anai-Guo

@Anai-Guo Anai-Guo commented Sep 6, 2026

Copy link
Copy Markdown

问题

GeminiClient._execute_response_requestmodels.generate_contenttry 块末尾有一组重复的 except 处理器,位于 except Exception as exc: 这个 catch-all 之后

        except Exception as exc:                 # 第 1121 行:捕获一切
            ...
            raise wrapped_error from exc
        except (UnknownFunctionCallArgumentError, UnsupportedFunctionError, FunctionInvocationError) as exc:  # 不可达
            raise RespParseException(None, f"Gemini 工具调用参数错误: {exc}") from exc
        except EmptyResponseException:           # 不可达
            raise
        except Exception as exc:                 # 不可达
            raise NetworkConnectionError(str(exc)) from exc

Python 的 except 子句按书写顺序自上而下匹配,一次只会命中一个。由于上方的 except Exception 已经捕获所有异常,这三个后置处理器永远不会执行

而且这几种异常在上方已经有更完整的处理器(第 1094 行处理 UnknownFunctionCallArgumentError/UnsupportedFunctionError/FunctionInvocationError,第 1108 行处理 EmptyResponseException),后置的这几行只是不带快照保存逻辑的旧版残留副本。

改动

删除这 6 行不可达代码,行为完全不变(它们本就无法被执行到)。

🤖 Generated with Claude Code

Summary by CodeRabbit

  • 错误处理
    • 改进 Gemini 请求失败时的错误信息,网络错误、响应解析失败和空响应现在会附带失败请求快照。
    • 已包含快照的异常将直接保留并重新抛出,避免重复包装。
    • 有助于更准确地定位请求失败原因并提升问题排查效率。

@coderabbitai

coderabbitai Bot commented Sep 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 5d61baf7-e2b1-4967-9e9a-b51428a6d549

📥 Commits

Reviewing files that changed from the base of the PR and between c13d9fb and e86cc55.

📒 Files selected for processing (1)
  • src/llm_models/model_client/gemini_client.py
💤 Files with no reviewable changes (1)
  • src/llm_models/model_client/gemini_client.py

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


Walkthrough

Gemini 响应请求的异常处理现在会保存失败请求快照,并将快照路径附加到网络错误、响应解析错误和空响应错误。已有快照的异常会直接重新抛出。

Changes

Gemini 错误处理

Layer / File(s) Summary
失败请求快照与异常包装
src/llm_models/model_client/gemini_client.py
通用异常处理会记录失败请求快照并附加快照路径。已有快照的异常直接重新抛出。其他异常转换为 NetworkConnectionError,同时保留 EmptyResponseExceptionRespParseException

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to e86cc

The change removes unreachable duplicate Gemini exception-handling code without altering the active request behavior. No merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning 描述清楚说明了问题、异常处理顺序、不可达代码及改动范围。但描述未按仓库模板提供分支确认、贡献指南确认、更新类型、测试结果、破坏性更新说明、关联 Issue 和其他信息等必需内容。 请补充并完成仓库模板中的各项内容。至少确认提交分支不是 main、已阅读贡献指南、更新类型为 BUG 修复、是否完成测试、破坏性更新情况、简要变更说明、关联 Issue 和附加信息。
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed 标题准确描述了主要变更:删除 generate_content 中位于 except Exception 之后的不可达重复异常处理代码。标题简洁且具体。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant