Skip to content

🧹 Refactor _oauth_error_response to reduce conditional nesting#138

Merged
DaTiC0 merged 2 commits into
mainfrom
refactor-oauth-error-response-nesting-13814294296413998890
Jun 6, 2026
Merged

🧹 Refactor _oauth_error_response to reduce conditional nesting#138
DaTiC0 merged 2 commits into
mainfrom
refactor-oauth-error-response-nesting-13814294296413998890

Conversation

@DaTiC0

@DaTiC0 DaTiC0 commented Jun 6, 2026

Copy link
Copy Markdown
Owner

🎯 What: The deeply nested conditionals in _oauth_error_response were flattened using guard clauses and early returns.
💡 Why: Deep nesting makes the code significantly harder to read, maintain, and reason about. Flattening it via guard clauses improves readability while preserving original functionality.
Verification: Ran pytest tests with an in-memory db setup which all passed (48 passed). Requested code review to verify that functionality and intended outcome is the same.
Result: Improved maintainability and clarity within the _oauth_error_response endpoint error formatting logic.


PR created automatically by Jules for task 13814294296413998890 started by @DaTiC0

Summary by Sourcery

Enhancements:

  • Flatten _oauth_error_response logic using guard clauses and a shared fallback helper to improve readability and maintainability.

This refactoring replaces a deeply nested if conditional in `_oauth_error_response` with guard clauses and early returns. This flattens the logical flow and makes the method much easier to read and maintain, without altering the functionality. A local `fallback` function is used to handle standard exit behavior to reduce duplication.

Co-authored-by: DaTiC0 <13198638+DaTiC0@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Refactors the _oauth_error_response helper to use early returns and a shared fallback function, reducing nested conditionals while preserving existing OAuth error redirect behavior.

Flow diagram for refactored _oauth_error_response logic

flowchart TD
    A[_oauth_error_response called with exc] --> B[Extract client_id, redirect_uri, state from request]
    B --> C[client = load_client client_id]
    C --> D{client exists?}
    D -- No --> Z[fallback: jsonify error and status_code]
    D -- Yes --> E{redirect_uri provided?}
    E -- No --> F[redirect_uri = client.get_default_redirect_uri]
    E -- Yes --> G[keep redirect_uri]
    F --> H{redirect_uri valid and client.check_redirect_uri?}
    G --> H{redirect_uri valid and client.check_redirect_uri?}
    H -- No --> Z
    H -- Yes --> I[parsed = urlparse redirect_uri]
    I --> J{parsed.scheme and parsed.netloc?}
    J -- No --> Z
    J -- Yes --> K[Build params with error, error_description, state]
    K --> L[Merge params into existing query]
    L --> M[safe_redirect_uri = urlunparse parsed]
    M --> N[redirect safe_redirect_uri]
    Z --> O[Return JSON error response]
Loading

File-Level Changes

Change Details Files
Refactor OAuth error response helper to use guard clauses and a shared fallback path instead of nested conditionals.
  • Introduced a local fallback() helper that builds the JSON error response using exc.error, exc.description, and a defensive status_code lookup with default 400.
  • Rewrote the client/redirect_uri handling logic to use sequential early returns when no client exists, when redirect_uri is missing or invalid, or when the parsed URI lacks scheme/netloc.
  • Simplified the construction of OAuth error parameters and query string merging while keeping the existing behavior for propagating error, error_description, and state and returning a redirect when possible.
routes.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the _oauth_error_response function in routes.py to flatten nested conditional logic using early returns and a nested helper function fallback(). The reviewer suggests replacing the nested fallback() function with a pre-constructed default error response tuple to avoid the overhead of recreating the function on every call and to simplify the code.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread routes.py Outdated
This refactoring replaces a deeply nested if conditional in `_oauth_error_response` with guard clauses and early returns. This flattens the logical flow and makes the method much easier to read and maintain, without altering the functionality. Pre-constructs the default return value directly to avoid unnecessary local function overhead.
@DaTiC0 DaTiC0 merged commit 45b4583 into main Jun 6, 2026
7 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.

1 participant