Skip to content

fix: error handling at storage level #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2024
Merged

fix: error handling at storage level #609

merged 1 commit into from
Dec 9, 2024

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Dec 9, 2024

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner December 9, 2024 19:54
Copy link

coderabbitai bot commented Dec 9, 2024

Walkthrough

The pull request introduces significant changes to multiple files in the internal/storage/ledger directory, primarily focusing on modifying method signatures to return tuples of (*bun.SelectQuery, error) instead of just *bun.SelectQuery. This change enhances error handling across various methods, ensuring that errors are propagated correctly and can be handled by the calling functions. The updates affect methods related to accounts, balances, moves, transactions, and volumes, improving the robustness of the ledger's functionality.

Changes

File Path Change Summary
internal/storage/ledger/accounts.go Updated method signatures for selectBalance, selectAccounts, ListAccounts, GetAccount, and CountAccounts to return (*bun.SelectQuery, error). Enhanced error handling throughout these methods.
internal/storage/ledger/balances.go Modified selectAccountWithAssetAndVolumes, selectAccountWithAggregatedVolumes, and SelectAggregatedBalances to return (*bun.SelectQuery, error). Improved error propagation in related methods.
internal/storage/ledger/moves.go Changed SortMovesBySeq and SelectDistinctMovesBySeq to return (*bun.SelectQuery, error), enhancing error handling.
internal/storage/ledger/transactions.go Updated selectTransactions, ListTransactions, CountTransactions, and GetTransaction to return (*bun.SelectQuery, error), improving error handling.
internal/storage/ledger/volumes.go Modified selectVolumes and GetVolumesWithBalances to return (*bun.SelectQuery, error), enhancing error handling capabilities.

Possibly related PRs

  • feat: optimize volumes endpoint when not using pit #608: The changes in internal/storage/ledger/volumes.go involve modifications to the selectVolumes function, which is also related to the error handling improvements seen in the main PR's updates to the selectAccounts and selectBalance methods in internal/storage/ledger/accounts.go. Both PRs enhance error handling by updating return types to include error signaling.

Suggested reviewers

  • flemzord
  • paul-nicolas

Poem

🐰 In the ledger's cozy nook,
Error handling took a look.
With tuples now in sight,
Queries shine so bright!
Robust and clear, they play their part,
A hop of joy, a coding art! 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
internal/storage/ledger/transactions.go (2)

Line range hint 53-242: LGTM! Error handling improvements look good.

The changes to return (*bun.SelectQuery, error) instead of using ret.Err(err) improve error handling by making it more explicit and consistent with Go's error handling patterns. The error propagation is now clearer and follows the standard Go practice of returning errors alongside results.

Consider adding error wrapping to provide more context about where the error occurred. For example:

-			return nil, err
+			return nil, fmt.Errorf("validating query: %w", err)

375-384: LGTM! Consider enhancing error context.

The error handling for selectTransactions is properly implemented. However, the subsequent database operation's error handling could be improved by adding more context.

Consider wrapping the database error with additional context:

 			if err := selectTransactions.
 				Where("transactions.id = ?", filter.ID).
 				Limit(1).
 				Model(ret).
 				Scan(ctx); err != nil {
-				return nil, postgres.ResolveError(err)
+				return nil, fmt.Errorf("scanning transaction: %w", postgres.ResolveError(err))
 			}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 471df93 and 6e34653.

📒 Files selected for processing (5)
  • internal/storage/ledger/accounts.go (10 hunks)
  • internal/storage/ledger/balances.go (4 hunks)
  • internal/storage/ledger/moves.go (3 hunks)
  • internal/storage/ledger/transactions.go (7 hunks)
  • internal/storage/ledger/volumes.go (6 hunks)
🔇 Additional comments (30)
internal/storage/ledger/balances.go (9)

Line range hint 21-154: Improved Error Handling in selectAccountWithAssetAndVolumes

The function selectAccountWithAssetAndVolumes now returns an error alongside the query. This enhancement allows for proper error propagation and handling throughout the call stack, increasing the robustness of the code.


55-55: Proper Error Propagation of Query Building Errors

Returning nil, err ensures that any errors encountered during query construction are correctly propagated to the caller.


60-60: Correct Feature Flag Check for Address Segments

The check for FeatureIndexAddressSegments and the corresponding error return maintain feature gating and prevent unintended usage when the feature is disabled.


67-68: Appropriate Error Handling for Missing Moves History Feature

By returning an error when FeatureMovesHistory is not enabled, the code prevents execution of dependent logic, which is essential for maintaining system integrity.


69-71: Handling Errors from SelectDistinctMovesBySeq

The code correctly captures and propagates errors returned by SelectDistinctMovesBySeq, ensuring that any issues are not silently ignored.


149-149: Enhanced Error Context with Error Wrapping

Using fmt.Errorf("building where clause: %w", err) provides additional context for the error, which aids in debugging and log analysis.


154-154: Consistent Return Statement in selectAccountWithAssetAndVolumes

The function concludes with return finalQuery, nil, adhering to the expected return types and ensuring clarity in successful execution paths.


157-166: Updated Error Handling in selectAccountWithAggregatedVolumes

The function now returns an error together with the query, and properly handles errors from selectAccountWithAssetAndVolumes. This change enhances consistency and reliability.


191-194: Proper Error Handling in GetAggregatedBalances

Errors from SelectAggregatedBalances are now correctly handled, allowing the function to appropriately respond to issues during data retrieval.

internal/storage/ledger/accounts.go (10)

45-58: Improved Error Handling in selectBalance

The function now returns an error along with the query. Errors from SelectDistinctMovesBySeq are properly checked and propagated, enhancing the function's robustness.


Line range hint 82-232: Enhanced Error Propagation in selectAccounts

By adjusting the return type to include an error, selectAccounts now properly propagates errors from internal calls like selectBalance and selectAccountWithAggregatedVolumes, which improves overall error handling in account queries.


111-111: Detailed Error Messaging in Filter Validation

The error message fmt.Errorf("failed to check filters: %w", err) adds clarity to filter validation failures, aiding in troubleshooting.


138-144: Correct Error Handling When Fetching Aggregated Volumes

Errors from selectAccountWithAggregatedVolumes are now properly handled, ensuring that the function reacts appropriately to issues during data aggregation.


171-175: Error Propagation in Dynamic Query Construction

The function now checks and returns errors from selectBalance when building dynamic queries, which is crucial for preventing silent failures during query execution.


223-223: Consistent Error Handling in Filter Evaluation

By returning an error with context fmt.Errorf("evaluating filters: %w", err), the code ensures consistent error propagation and easier debugging.


232-232: Consistent Success Return in selectAccounts

The function ends with return ret, nil, which clearly indicates successful execution without errors.


236-244: Error Handling in ListAccounts Function

The ListAccounts function now properly handles errors returned by selectAccounts, ensuring that any issues are communicated to the caller.


274-278: Proper Error Checking in GetAccount

Errors from selectAccounts are appropriately handled, allowing GetAccount to respond correctly to failures in account retrieval.


298-306: Error Propagation in CountAccounts

The function correctly handles errors from selectAccounts, maintaining consistency in error management across account-related functions.

internal/storage/ledger/moves.go (3)

Line range hint 15-31: Adjusted Return Type in SortMovesBySeq for Error Handling

The function now returns an error along with the query. This change allows for proper error propagation, especially when the required feature is not enabled.


34-38: Error Handling in SelectDistinctMovesBySeq

Errors from SortMovesBySeq are now correctly handled and propagated, ensuring that upstream functions are aware of and can handle any issues.


50-50: Consistent Return Statement in SelectDistinctMovesBySeq

Returning ret, nil maintains consistency in the success path, clearly indicating that the function executed without errors.

internal/storage/ledger/volumes.go (6)

Line range hint 67-235: Improved Error Handling in selectVolumes Function

The function selectVolumes now returns an error along with the query. This enhancement ensures that any issues during query construction are properly communicated to the caller.


101-101: Consistent Error Propagation in Query Building

By returning nil, err, the function ensures that errors encountered during query building are correctly propagated, aligning with Go error-handling best practices.


118-118: Feature Flag Verification for Move History

The check for FeatureMovesHistory with an appropriate error return prevents the use of move history features when they are not enabled.


213-213: Error Handling During Filter Parsing

Errors encountered while parsing filters are properly returned, which improves the reliability of query executions.


235-235: Consistent Return Statement in selectVolumes

The function concludes with return globalQuery, nil, providing clarity on the successful execution path.


245-254: Error Handling in GetVolumesWithBalances

Errors from selectVolumes are now correctly handled in GetVolumesWithBalances, ensuring that any issues in volume retrieval are properly communicated.

internal/storage/ledger/transactions.go (2)

320-328: LGTM! Proper error handling from selectTransactions.

The error handling is properly implemented, checking and propagating errors from the selectTransactions call before proceeding with cursor creation.


350-358: LGTM! Proper error handling from selectTransactions.

The error handling is properly implemented, checking and propagating errors from the selectTransactions call before proceeding with the count query.

@gfyrag gfyrag added this pull request to the merge queue Dec 9, 2024
Merged via the queue into main with commit ed8b08d Dec 9, 2024
8 checks passed
@gfyrag gfyrag deleted the fix/error-handling branch December 9, 2024 20:16
This was referenced Dec 10, 2024
@coderabbitai coderabbitai bot mentioned this pull request Feb 17, 2025
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