refactor: unwrap response util#1036
Conversation
WalkthroughThis pull request introduces a centralized Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
Summary of ChangesHello @leonardmq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors how API responses are handled throughout the application. By introducing a dedicated Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
📊 Coverage ReportOverall Coverage: 91% Diff: origin/sfierro/optimize-feature...HEAD
Summary
Line-by-lineView line-by-line diff coverageapp/desktop/studio_server/copilot_api.pyLines 205-213 205 client=client,
206 body=questioner_input,
207 )
208 )
! 209 result = unwrap_response(
210 detailed_result,
211 none_detail="Failed to generate clarifying questions for spec. Please try again.",
212 )Lines 231-239 231 detailed_result = await refine_spec_with_answers_v1_copilot_refine_spec_with_answers_post.asyncio_detailed(
232 client=client,
233 body=submit_input,
234 )
! 235 result = unwrap_response(
236 detailed_result,
237 none_detail="Failed to refine spec with question answers. Please try again.",
238 )app/desktop/studio_server/utils/copilot_utils.pyLines 90-98 90 client=client,
91 body=generate_input,
92 )
93 )
! 94 result = unwrap_response(
95 detailed_result,
96 none_detail="Failed to generate synthetic data for spec. Please try again.",
97 )
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
app/desktop/studio_server/utils/response_utils.py (1)
46-49:RuntimeErrorvsHTTPExceptioninconsistency for the HTTPValidationError guard.This branch raises
RuntimeErrorwhile every other error path in this module raisesHTTPException. If this ever fires in production (e.g., due to an SDK codegen quirk), the caller won't get a clean JSON error — FastAPI will produce a generic 500 with"Internal Server Error"text instead of the structured{"detail": ...}format the rest of the codebase relies on.Since you already acknowledge this "should never happen," switching to
HTTPExceptionkeeps the contract uniform at minimal cost.Proposed fix
if isinstance(parsed_response, HTTPValidationError): - raise RuntimeError("An unknown error occurred.") + raise HTTPException(status_code=500, detail="An unknown error occurred.")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/desktop/studio_server/utils/response_utils.py` around lines 46 - 49, The guard that currently raises RuntimeError when parsed_response is an instance of HTTPValidationError should instead raise an HTTPException to keep error responses consistent; in the function that contains parsed_response and the HTTPValidationError check (and near check_response_error), replace the RuntimeError raise with raising fastapi.HTTPException(status_code=400, detail=...) or another suitable status and include a descriptive detail string (e.g., "Validation error") so the caller receives the same structured JSON error format as other branches.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@app/desktop/studio_server/utils/response_utils.py`:
- Around line 46-49: The guard that currently raises RuntimeError when
parsed_response is an instance of HTTPValidationError should instead raise an
HTTPException to keep error responses consistent; in the function that contains
parsed_response and the HTTPValidationError check (and near
check_response_error), replace the RuntimeError raise with raising
fastapi.HTTPException(status_code=400, detail=...) or another suitable status
and include a descriptive detail string (e.g., "Validation error") so the caller
receives the same structured JSON error format as other branches.
There was a problem hiding this comment.
Code Review
This pull request introduces a new unwrap_response utility to centralize and simplify API response handling, significantly reducing boilerplate code. A critical security concern was identified regarding sensitive information exposure in the error handling logic of the prompt optimization job API, where internal exception messages are returned directly to the user. It is crucial to use generic error messages for user-facing responses and log detailed exceptions internally to prevent leakage of sensitive system details. Additionally, ensure specific error messages for None responses are preserved by passing the none_detail argument to unwrap_response where appropriate, to maintain the quality of error reporting.
What does this PR do?
Changes:
asyncio_detailedandcheck_error_responseeverywherecheck_error_responseHTTPValidationError, check parsed is notNone, etc.)check_error_responseuse the new utilChecklists
Summary by CodeRabbit
Refactor
Tests