Skip to content
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

feat(sdk): consensus error 2 #2275

Closed
wants to merge 1 commit into from

Conversation

shumkov
Copy link
Member

@shumkov shumkov commented Oct 24, 2024

Issue being fixed or feature implemented

What was done?

How Has This Been Tested?

Breaking Changes

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Summary by CodeRabbit

  • New Features

    • Enhanced error handling capabilities in the SDK, allowing for more detailed error reporting and improved granularity in error information.
  • Bug Fixes

    • Updated error type handling to support downcasting and improved error propagation for specific consensus errors.

Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

Walkthrough

The pull request introduces modifications to error handling in the TransportClient trait and the SDK's error module. The Error type in the TransportClient trait now includes a 'static lifetime bound, ensuring its validity throughout the program's execution. Additionally, the From<DapiClientError<T>> for Error implementation has been updated to include the std::any::Any trait bound, enabling enhanced error reporting and downcasting capabilities for specific error types. Overall, these changes improve the granularity and flexibility of error handling in the SDK.

Changes

File Path Change Summary
packages/rs-dapi-client/src/transport.rs Updated Error type in TransportClient trait to include 'static lifetime bound.
packages/rs-sdk/src/error.rs Enhanced error handling by adding std::any::Any trait bound to From<DapiClientError<T>> for Error. Added logic to deserialize specific transport errors into ConsensusError and wrap them in ProtocolError.

Possibly related PRs

Suggested labels

enhancement, js-sdk

Suggested reviewers

  • QuantumExplorer
  • lklimek

🐰 In the garden of code, we hop with delight,
Errors now clearer, shining so bright.
With traits that can downcast, and lifetimes so grand,
Our SDK's ready, with a helping hand!
Let's celebrate changes, both clever and neat,
For every new feature makes our work sweet! 🌼✨


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 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.

@shumkov shumkov marked this pull request as draft October 24, 2024 16:23
Copy link
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (1)
packages/rs-dapi-client/src/transport.rs (1)

51-51: LGTM! Addition of 'static bound enables robust error handling.

The addition of the 'static bound is a good architectural decision as it:

  • Enables downcasting capabilities through the Any trait
  • Ensures errors can be safely stored and handled across async boundaries
  • Aligns with Rust's standard error handling patterns
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e0e152f and 00f36c7.

📒 Files selected for processing (2)
  • packages/rs-dapi-client/src/transport.rs (1 hunks)
  • packages/rs-sdk/src/error.rs (2 hunks)
🔇 Additional comments (3)
packages/rs-dapi-client/src/transport.rs (1)

51-51: Verify existing TransportClient implementations.

Since adding 'static bound is a breaking change, we should verify all existing implementations of TransportClient to ensure their Error types comply with the new constraint.

✅ Verification successful

Let me gather more specific information about the TransportClient implementations to verify their Error types.

The first script will help us find all implementations of TransportClient and their Error type definitions. The second script will show us the trait definition to understand the constraints. The third script will help us understand any custom Error types defined in the transport module. This will allow us to verify if the 'static bound addition is a breaking change.


Let me try a different approach to find the implementations and error types.

This should help us find:

  1. The TransportClient trait definition and its requirements
  2. Any implementations of TransportClient
  3. Files containing Error type definitions that might be related to TransportClient implementations

Let me verify if the Error type being used (dapi_grpc::tonic::Status) already implements the required traits.

This will help us verify if the Error type used in the implementations (tonic::Status) already satisfies the 'static bound and other trait requirements.


Let me check the Mockable trait implementation and verify if tonic::Status is already 'static.

This will help us verify if the Error type (tonic::Status) already implements Mockable and if it's inherently 'static, which would mean the change isn't breaking.


Based on the gathered information, I can now provide a final response:

Adding 'static bound is not a breaking change in this case

The existing implementations of TransportClient use dapi_grpc::tonic::Status as their Error type, which:

  1. Already implements CanRetry (verified in transport/grpc.rs)
  2. Already implements Mockable (verified in dapi-grpc/src/mock.rs)
  3. Is inherently 'static as it's a standard error type from tonic

The change is safe because:

  • Both existing implementations (PlatformGrpcClient and CoreGrpcClient) use tonic::Status which already satisfies all bounds including 'static
  • No other implementations were found that might be affected by this change
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all TransportClient implementations to verify their Error types
ast-grep --pattern 'impl $_ for $type {
  type Error = $err;
  $$$
}'

# Also search for potential custom Error type definitions
rg -l "pub.*enum.*Error|pub.*struct.*Error" 

Length of output: 31089


Script:

#!/bin/bash
# Search for TransportClient implementations and their Error type definitions
ast-grep --pattern 'impl $_ for $type {
  type Error = $err;
  $$$
}'

# Search for TransportClient trait definition
ast-grep --pattern 'trait TransportClient {
  $$$
}'

# Search for specific Error type definitions in the transport module
rg -A 5 "pub.*enum.*Error|pub.*struct.*Error" packages/rs-dapi-client/src/transport.rs

Length of output: 215


Script:

#!/bin/bash
# Search for the TransportClient trait and its implementations
rg -A 10 "trait TransportClient" packages/rs-dapi-client/src/

# Search for impl blocks that might be implementing TransportClient
rg -A 10 "impl.*TransportClient" packages/rs-dapi-client/src/

# Look for Error type definitions in files that might implement TransportClient
fd . packages/rs-dapi-client/src/ -t f -x rg -l "type Error.*=.*;"

Length of output: 3219


Script:

#!/bin/bash
# Check if tonic::Status implements the required traits
rg "impl.*CanRetry.*for.*Status" packages/rs-dapi-client/src/
rg "impl.*Mockable.*for.*Status" packages/rs-dapi-client/src/

# Look for trait implementations in the crate where these traits are defined
fd . -t f -x rg -l "impl.*for.*Status"

Length of output: 872


Script:

#!/bin/bash
# Check for Mockable implementation for Status
rg -A 5 "impl.*Mockable.*for.*Status" packages/dapi-grpc/src/mock.rs

# Check if Status is already marked as 'static in its definition
rg "pub.*struct.*Status" packages/dapi-grpc/src/

# Check if the Mockable trait is implemented elsewhere for Status
rg "impl.*Mockable.*for.*Status" --type rust

Length of output: 496

packages/rs-sdk/src/error.rs (2)

2-9: LGTM: New imports are properly utilized

The added imports support the enhanced error handling capabilities, particularly for consensus errors.


80-97: Verify the consensus error handling integration

The implementation correctly extracts and deserializes consensus errors from gRPC metadata. Let's verify the integration with the gRPC server.

Comment on lines +87 to +94
return ConsensusError::deserialize_from_bytes(
consensus_error_value.as_encoded_bytes(),
)
.map(|consensus_error| {
Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
})
.unwrap_or_else(Error::Protocol);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider enhancing error handling granularity

The current implementation silently falls back to a generic Protocol error if deserialization fails. Consider providing more specific error information in failure cases.

-    return ConsensusError::deserialize_from_bytes(
-        consensus_error_value.as_encoded_bytes(),
-    )
-    .map(|consensus_error| {
-        Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
-    })
-    .unwrap_or_else(Error::Protocol);
+    return match ConsensusError::deserialize_from_bytes(
+        consensus_error_value.as_encoded_bytes(),
+    ) {
+        Ok(consensus_error) => {
+            Error::Protocol(ProtocolError::ConsensusError(Box::new(consensus_error)))
+        }
+        Err(e) => Error::Protocol(ProtocolError::Generic(format!(
+            "Failed to deserialize consensus error: {}",
+            e
+        ))),
+    };

Committable suggestion was skipped due to low confidence.

if let DapiClientError::Transport(ref t, _) = &value {
if let Some(status) = (t as &dyn Any).downcast_ref::<dapi_grpc::tonic::Status>() {
if let Some(consensus_error_value) =
status.metadata().get_bin("drive-error-data-bin")
Copy link
Member Author

Choose a reason for hiding this comment

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

Currently cbor is inside. We should do it without cbor

@shumkov
Copy link
Member Author

shumkov commented Oct 25, 2024

Close in favor of #2274

@shumkov shumkov closed this Oct 25, 2024
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