Skip to content

chore: improve Cursor error delivery and defer logic#1050

Merged
almas-x merged 3 commits intomasterfrom
almas/chore
May 28, 2025
Merged

chore: improve Cursor error delivery and defer logic#1050
almas-x merged 3 commits intomasterfrom
almas/chore

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented May 27, 2025

📑 Description

What’s Changed

  • Refactored Query.Cursor() to simplify the return signature by removing the (chan, error) tuple.
  • Now, errors during row iteration are sent through the channel by wrapping them in a Row struct with the err field set.
  • Deferred cleanup of resources (channel close and Rows.Close()) is handled reliably even in case of early errors.
  • Prevents potential goroutine leaks and ensures consumers always receive a closed channel.

Why

Previously, errors in goroutine setup (e.g., query.instance.Rows()) were only returned via the outer error return, which could be misleading since the channel would never yield results. This refactor ensures a more Go-idiomatic pattern: the channel always closes, and all consumers can handle errors uniformly through the same channel.

Potential Impact

Downstream consumers of Cursor() should now check Row.err to determine if an error occurred, instead of relying on the function's return error.

Example Usage (Updated)

for row := range query.Cursor() {
	if row.Err() != nil {
		log.Println("cursor error:", row.Err())
		break
	}
	// Process row
}

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings May 27, 2025 08:15
@almas-x almas-x requested a review from a team as a code owner May 27, 2025 08:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

The PR refactors the Cursor method to remove its error return and instead send any iteration or setup errors through the returned channel, while also ensuring proper resource cleanup.

  • Simplifies Cursor signature to chan Row and delivers errors via a Row.err field.
  • Adds Err() to the Row struct/interface and updates mocks accordingly.
  • Ensures channel closure and resource deferral even on early errors.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/query_test.go Updated test to drop error return from Cursor() and assert row.Err()
tests/go.mod Bumped indirect google.golang.org/genproto dependency
mocks/database/orm/Query.go Removed error return from mocked Cursor() signature
mocks/database/db/Row.go Added mock support for new Row.Err() method
database/gorm/row.go Added err field and Err() method to Row; guard Scan on err
database/gorm/query.go Refactored Cursor() to push errors into the channel and defer cleanup
contracts/database/orm/orm.go Changed Cursor() signature to drop error return
contracts/database/db/db.go Added Err() to Row interface
Comments suppressed due to low confidence (2)

database/gorm/query.go:176

  • Errors that occur on the initial rows.Next() call (before entering the loop) aren’t checked, so they may be silently dropped. After the loop, add a check for rows.Err() (e.g., if errRows := rows.Err(); errRows != nil { err = errRows }) to ensure all iteration errors are delivered.
for rows.Next() {

tests/query_test.go:577

  • [nitpick] There’s no test covering the error delivery path of Cursor(). Consider adding a test that simulates an error from Rows() or ScanRows() and asserts that row.Err() yields that error.
func (s *QueryTestSuite) TestCursor() {

@codecov
Copy link

codecov bot commented May 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.54%. Comparing base (727d072) to head (b98bd43).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1050   +/-   ##
=======================================
  Coverage   70.54%   70.54%           
=======================================
  Files         176      176           
  Lines       12330    12330           
=======================================
  Hits         8698     8698           
  Misses       3252     3252           
  Partials      380      380           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

LGTM, can we add some test cases for the error flow?

@almas-x
Copy link
Contributor Author

almas-x commented May 27, 2025

LGTM, can we add some test cases for the error flow?

I’ll add some basic error flow tests. Other scenarios are hard to simulate without mocking or a more complex setup.

@almas-x almas-x requested a review from hwbrzzl May 27, 2025 09:46
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Great PR 👍 Could you fix the gorm query for the v1.15.x branch as well? They are a bit different, only need to fix the memory leak, don't make a breaking change for the Cursor function.

@almas-x
Copy link
Contributor Author

almas-x commented May 28, 2025

Great PR 👍 Could you fix the gorm query for the v1.15.x branch as well? They are a bit different, only need to fix the memory leak, don't make a breaking change for the Cursor function.

Okay

@almas-x almas-x merged commit 577dc83 into master May 28, 2025
14 checks passed
@almas-x almas-x deleted the almas/chore branch May 28, 2025 01:41
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.

3 participants