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

refactor: reduce use of unwrap #54

Merged
merged 4 commits into from
Jul 16, 2024
Merged

refactor: reduce use of unwrap #54

merged 4 commits into from
Jul 16, 2024

Conversation

kwaa
Copy link
Member

@kwaa kwaa commented Jul 15, 2024

Summary by CodeRabbit

  • Refactor

    • Improved URL parsing and error handling to enhance robustness.
    • Updated asynchronous operations to use TryJoinAll for better error control.
    • Enhanced JSON serialization/deserialization to handle errors more gracefully.
  • Bug Fixes

    • Fixed potential crashes by replacing unwrap() calls with safer error handling methods.

Copy link

coderabbitai bot commented Jul 15, 2024

Walkthrough

The changes primarily enhance error handling across several modules by replacing direct unwrap calls with safer alternatives like filter_map(Result::ok) and leveraging TryJoinAll for more robust asynchronous operations. These adjustments aim to reduce runtime panics and improve the overall resilience and maintainability of the codebase.

Changes

File(s) Change Summary
.../api_apub/src/users/user_followers.rs, .../users/user_outbox.rs Changed .unwrap() calls to .filter_map(Result::ok) for safer URL and JSON handling.
.../api_mastodon/src/entities/context.rs, .../routes/statuses/status_favourited_by.rs, .../status_reblogged_by.rs Introduced TryJoinAll from the futures crate, replacing join_all for better asynchronous operation handling.
.../apub/src/actors/db_user.rs Enhanced URL parsing and image creation using and_then and map_or to handle errors more gracefully.
.../apub/src/actors/db_user_impl.rs Improved error handling with the ? operator and introduced TryJoinAll for async operations.
.../feed/src/user_feed_item.rs Updated mapping of tags from JSON using and_then with serde_json methods, avoiding direct unwrapping.

Poem

In code, where errors used to creep,
We now have filters, gentle and deep.
No more panics, just calm flows,
Async tasks now join in shows.
With TryJoinAll and graceful parse,
Our code now sings, like a rabbit in the grass.
❀ Hop, hop, hooray, for a safer code array! ❀


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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

@kwaa kwaa marked this pull request as ready for review July 16, 2024 01:23
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 387b653 and e7df5d4.

Files selected for processing (8)
  • crates/api_apub/src/users/user_followers.rs (1 hunks)
  • crates/api_apub/src/users/user_outbox.rs (1 hunks)
  • crates/api_mastodon/src/entities/context.rs (2 hunks)
  • crates/api_mastodon/src/routes/statuses/status_favourited_by.rs (2 hunks)
  • crates/api_mastodon/src/routes/statuses/status_reblogged_by.rs (2 hunks)
  • crates/apub/src/actors/db_user.rs (1 hunks)
  • crates/apub/src/actors/db_user_impl.rs (2 hunks)
  • crates/feed/src/user_feed_item.rs (2 hunks)
Additional comments not posted (8)
crates/api_mastodon/src/entities/context.rs (1)

30-42: Refactor to use TryJoinAll improves error handling and control flow.

The change from a direct collection of Status objects to using TryJoinAll ensures that errors in any of the asynchronous operations are properly propagated, preventing partial success states and improving overall robustness of the method.

crates/api_mastodon/src/routes/statuses/status_favourited_by.rs (1)

41-51: Use of TryJoinAll in status_favourited_by enhances error propagation.

The replacement of a simpler collection pattern with TryJoinAll for handling related ReceivedLike entities ensures that all operations must succeed for the result to be considered valid. This change enhances error handling by not allowing partial successes which could lead to inconsistent states.

crates/api_mastodon/src/routes/statuses/status_reblogged_by.rs (1)

41-51: Consistent application of TryJoinAll in status_reblogged_by route.

Implementing TryJoinAll in the handling of ReceivedAnnounce entities ensures that errors in any part of the asynchronous operation are caught and handled appropriately, maintaining consistency with the changes in other parts of the codebase.

crates/api_apub/src/users/user_followers.rs (1)

91-92: Improved error handling with map and filter_map(Result::ok) in URL parsing.

The refactoring to handle URL parsing errors more gracefully by using map and filter_map(Result::ok) instead of direct unwrapping prevents potential runtime panics due to malformed URLs, enhancing the robustness of the application.

crates/api_apub/src/users/user_outbox.rs (1)

95-97: Improved error handling in activity JSON conversion.

The change from directly unwrapping the JSON conversion result to using filter_map(Result::ok) is a significant improvement. It prevents potential runtime panics that could occur if the conversion fails, thus enhancing the robustness of the code.

crates/apub/src/actors/db_user_impl.rs (1)

96-108: Enhanced error handling and concurrency in activity sending.

The use of TryJoinAll for handling multiple asynchronous operations concurrently is a robust improvement. It ensures that all operations either succeed or fail together, providing better error propagation and simplifying error handling.

crates/feed/src/user_feed_item.rs (1)

73-73: Improved error handling in tag serialization and deserialization.

The use of and_then with serde_json::from_str and serde_json::to_string for handling tag data is an excellent improvement. It ensures that errors in data format do not cause runtime exceptions, thus enhancing the robustness and reliability of the code.

Also applies to: 101-101

crates/apub/src/actors/db_user.rs (1)

131-135: Enhanced error handling in URL parsing for icons and images.

The changes to use and_then with Url::parse followed by map_or for handling potential errors in URL parsing for user icons and images are commendable. This approach enhances error handling by gracefully managing parsing failures, thus preventing runtime exceptions.

@kwaa kwaa merged commit eaa8730 into main Jul 16, 2024
1 check passed
@kwaa kwaa deleted the refactor/unwrap branch July 16, 2024 01:26
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.

None yet

1 participant