Skip to content

Resolve next page cursor loop - #188

Closed
Nidhi091999 wants to merge 5 commits into
devfrom
nidhi/issue-165
Closed

Nidhi091999 wants to merge 5 commits into
devfrom
nidhi/issue-165

Conversation

@Nidhi091999

@Nidhi091999 Nidhi091999 commented Aug 4, 2025 •

Copy link
Copy Markdown
Contributor

Fix(pagination): resolve next page cursor loop

Summary by Sourcery

Resolve infinite pagination loop and extend error handling

Bug Fixes:

  • Fix pagination in list_tasks to fetch page_size+1 items, set next_page_token only when additional items exist, and trim the results to page_size

Enhancements:

  • Introduce new exception classes (InputUriError, IPDistanceCalculationError, TesUriError) and update the central exception mapping with corresponding HTTP messages and status codes

@sourcery-ai

sourcery-ai Bot commented Aug 4, 2025 •

Copy link
Copy Markdown

Reviewer's Guide

Adjusts pagination logic to prevent infinite loops by fetching an extra record and refactors the exceptions module by reordering definitions, introducing new error classes (InputUriError, IPDistanceCalculationError, TesUriError), and updating the error mapping.

Class diagram for updated exception classes

classDiagram
    class InputUriError {
        <<ValueError>>
    }
    class IPDistanceCalculationError {
        <<ValueError>>
    }
    class TesUriError {
        <<ValueError>>
    }
    class NoTesInstancesAvailable {
        <<ValueError>>
    }
    class TaskNotFound {
        <<NotFound>>
    }
    class IdsUnavailableProblem {
        <<PyMongoError>>
    }
    class MiddlewareException {
        <<ValueError>>
    }
    class InvalidMiddleware {
        <<MiddlewareException>>
    }
    InputUriError --|> ValueError
    IPDistanceCalculationError --|> ValueError
    TesUriError --|> ValueError
    NoTesInstancesAvailable --|> ValueError
    TaskNotFound --|> NotFound
    IdsUnavailableProblem --|> PyMongoError
    InvalidMiddleware --|> MiddlewareException
    MiddlewareException --|> ValueError
Loading

File-Level Changes

Change Details Files
Refactor exception definitions and update error mapping
  • Reordered TaskNotFound and NoTesInstancesAvailable definitions
  • Introduced InputUriError, IPDistanceCalculationError, and TesUriError classes
  • Added mapping entries for TesUriError, InputUriError, and IPDistanceCalculationError
pro_tes/exceptions.py
Fix pagination to resolve next page cursor loop
  • Increase database query limit by one to detect additional pages
  • Adjust next_page_token logic to use the extra record and trim the result list
pro_tes/ga4gh/tes/task_runs.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

Choose a reason for hiding this comment

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

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

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `pro_tes/ga4gh/tes/task_runs.py:297` </location>
<code_context>
         logger.debug(f"Tasks list: {tasks_list}")
-        if tasks_list:
-            next_page_token = str(tasks_list[-1]["_id"])
+        if len(tasks_list) > page_size:
+            next_page_token = str(tasks_list[page_size-1]["_id"])
+            tasks_list = tasks_list[:page_size]
         else:
</code_context>

<issue_to_address>
The logic for next_page_token may skip the correct item if page_size is 0.

Accessing tasks_list[page_size-1] when page_size is 0 retrieves the last element, which is likely unintended. Please add a check for page_size == 0 to prevent this issue.
</issue_to_address>

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.

Comment on lines +297 to +298
if len(tasks_list) > page_size:
next_page_token = str(tasks_list[page_size-1]["_id"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): The logic for next_page_token may skip the correct item if page_size is 0.

Accessing tasks_list[page_size-1] when page_size is 0 retrieves the last element, which is likely unintended. Please add a check for page_size == 0 to prevent this issue.

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