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

fix: ensure metadata loading errors are correctly logged #2149

Merged
merged 1 commit into from
Jul 7, 2024

Conversation

glihm
Copy link
Collaborator

@glihm glihm commented Jul 7, 2024

Description

To ensure smooth transition to 1.0.0, this PR ensures that the errors from incorrect configuration are guiding the user to make the according changes.

Summary by CodeRabbit

  • Refactor

    • Improved error handling across various commands and functions by adding ? operator for more robust error propagation.
  • Tests

    • Updated test cases to handle potential errors more gracefully by adding .unwrap() where necessary to ensure valid function call results.
  • Documentation

    • Enhanced error context messages for metadata retrieval and parsing.

Copy link

coderabbitai bot commented Jul 7, 2024

Walkthrough

The recent changes primarily involve adding error handling mechanisms across various files in the codebase, specifically utilizing the ? operator for propagating errors. Additionally, some functions have been refactored to return Result types for better error handling, and certain test cases now use .unwrap() to handle results where applicable. These updates aim to improve the code's robustness and error resilience.

Changes

Files/Groups Change Summaries
bin/sozo/src/commands/auth.rs, call.rs, execute.rs, model.rs Added ? for error propagation in function calls to get_default_namespace_from_ws(&ws).
bin/sozo/src/commands/build.rs Refactored Dojo target checks and metadata handling; improved bindgen generation.
crates/dojo-lang/src/compiler.rs, metadata.rs Updated function calls with ? operator and enhanced error messages for metadata handling functions.
bin/sozo/tests/register_test.rs, crates/sozo/ops/src/.../tests Added .unwrap() to handle results from get_default_namespace_from_ws(&ws) in test files.
crates/sozo/ops/src/migration/mod.rs, crates/sozo/ops/src/setup.rs Incorporated ? for error handling and added .unwrap() for result handling in migrations and setup.
crates/torii/core/src/sql_test.rs, crates/torii/graphql/src/tests, crates/torii/grpc/src/.../entities_test.rs Modified test error handling by adding .unwrap() to get_default_namespace_from_ws(&ws) calls.

Poem

In code's vast fields, improvements abound,
The ? now dances where errors are found.
Unwrapping truths in every test's line,
A Dojo refactor, now gracefully fine.
🐇 CodeRabbit hops with glee,
Stronger code for all to see!
🎉


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.

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: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ed9f0ce and aac2854.

Files selected for processing (14)
  • bin/sozo/src/commands/auth.rs (1 hunks)
  • bin/sozo/src/commands/build.rs (2 hunks)
  • bin/sozo/src/commands/call.rs (1 hunks)
  • bin/sozo/src/commands/execute.rs (1 hunks)
  • bin/sozo/src/commands/model.rs (1 hunks)
  • bin/sozo/tests/register_test.rs (1 hunks)
  • crates/dojo-lang/src/compiler.rs (1 hunks)
  • crates/dojo-world/src/metadata.rs (3 hunks)
  • crates/sozo/ops/src/migration/mod.rs (1 hunks)
  • crates/sozo/ops/src/tests/migration.rs (3 hunks)
  • crates/sozo/ops/src/tests/setup.rs (1 hunks)
  • crates/torii/core/src/sql_test.rs (2 hunks)
  • crates/torii/graphql/src/tests/mod.rs (1 hunks)
  • crates/torii/grpc/src/server/tests/entities_test.rs (1 hunks)
Files skipped from review due to trivial changes (1)
  • bin/sozo/tests/register_test.rs
Additional comments not posted (15)
bin/sozo/src/commands/model.rs (1)

116-116: Ensure proper error handling when retrieving the default namespace.

The addition of the ? operator ensures that errors are propagated correctly. Make sure to handle these errors appropriately in the calling context.

crates/sozo/ops/src/migration/mod.rs (1)

81-81: Ensure proper error handling when retrieving the default namespace.

The addition of the ? operator ensures that errors are propagated correctly. Make sure to handle these errors appropriately in the calling context.

bin/sozo/src/commands/build.rs (2)

63-63: Ensure proper error handling when retrieving Dojo metadata.

The addition of the ? operator ensures that errors are propagated correctly. Make sure to handle these errors appropriately in the calling context.


146-149: Ensure proper error handling in async context.

The use of unwrap and expect in the async context can lead to panics if errors occur. Consider handling these errors more gracefully.

Do you want me to suggest a more graceful error handling approach for this async context?

crates/torii/graphql/src/tests/mod.rs (1)

294-294: Ensure proper error handling in test scenarios.

The use of unwrap ensures that errors are not silently ignored in test scenarios. Make sure that the test setup is robust enough to handle potential errors.

bin/sozo/src/commands/auth.rs (1)

67-67: Ensure proper error handling with ? operator.

The addition of the ? operator to the get_default_namespace_from_ws(&ws) function call ensures that any errors are propagated correctly. This is a good practice for handling errors in Rust.

crates/torii/core/src/sql_test.rs (2)

80-80: Ensure proper error handling with .unwrap() in test scenarios.

The addition of the .unwrap() method to the get_default_namespace_from_ws(&ws) function call ensures that any errors are handled in test scenarios. While this is acceptable in test code, ensure that the tests are designed to handle potential errors gracefully.


225-225: Ensure proper error handling with .unwrap() in test scenarios.

The addition of the .unwrap() method to the get_default_namespace_from_ws(&ws) function call ensures that any errors are handled in test scenarios. While this is acceptable in test code, ensure that the tests are designed to handle potential errors gracefully.

crates/dojo-world/src/metadata.rs (3)

56-58: Ensure proper error handling with Result return type.

The get_default_namespace_from_ws function now returns a Result<String>, which enhances error handling by allowing the function to propagate errors. This is a good practice for handling errors in Rust.


101-112: Ensure proper error context messages.

The improved error context messages in the dojo_metadata_from_workspace function provide more descriptive error messages, making it easier to diagnose issues. This is a good practice for improving error handling.


445-452: Ensure proper error context messages in MetadataExt implementation.

The improved error context messages in the MetadataExt implementation provide more descriptive error messages, making it easier to diagnose issues. This is a good practice for improving error handling.

crates/sozo/ops/src/tests/migration.rs (3)

208-208: Ensure proper error handling with .unwrap() in test scenarios.

The addition of the .unwrap() method to the get_default_namespace_from_ws(&ws) function call ensures that any errors are handled in test scenarios. While this is acceptable in test code, ensure that the tests are designed to handle potential errors gracefully.


378-378: Ensure proper error handling with .unwrap() in test scenarios.

The addition of the .unwrap() method to the get_default_namespace_from_ws(&ws) function call ensures that any errors are handled in test scenarios. While this is acceptable in test code, ensure that the tests are designed to handle potential errors gracefully.


411-411: Ensure proper error handling with .unwrap() in test scenarios.

The addition of the .unwrap() method to the get_default_namespace_from_ws(&ws) function call ensures that any errors are handled in test scenarios. While this is acceptable in test code, ensure that the tests are designed to handle potential errors gracefully.

crates/dojo-lang/src/compiler.rs (1)

90-90: LGTM! But verify the function usage in the codebase.

The code changes are approved. Propagating errors using the ? operator ensures that errors are not silently ignored.

However, ensure that all function calls to compile handle the propagated errors correctly.

Verification successful

Verify error handling for compile function calls in dojo-lang crate.

We need to ensure that all calls to the compile function in crates/dojo-lang/src/compiler.rs handle the propagated errors correctly. We'll refine our search to focus on the specific function and its usage within the dojo-lang crate.


Re-verify the function usage and error handling for compile function calls.

We need to re-verify the function calls to compile and ensure proper error handling. This time, we'll search across all crates to ensure comprehensive coverage.


Re-verify the function usage and error handling for compile function calls using rg.

We need to re-verify the function calls to compile and ensure proper error handling using rg for a broader search scope.


Review the function usage and error handling for compile function calls.

The search results indicate multiple compile function definitions and calls. Specifically, we need to ensure that the compile function in crates/dojo-lang/src/compiler.rs is handled correctly.

  • Function Definition:

    • crates/dojo-lang/src/compiler.rs: fn compile(&self, unit: CairoCompilationUnit, db: &mut RootDatabase, ws: &Workspace<'_>) -> Result<()> {
  • Function Calls:

    • crates/dojo-lang/src/scarb_internal/mod.rs: if let Err(err) = ws.config().compilers().compile(unit.clone(), &mut (db), &ws) {

The function call in crates/dojo-lang/src/scarb_internal/mod.rs uses error handling with if let Err(err), which is correct.

Conclusion: The function calls to compile in the dojo-lang crate handle errors correctly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `compile` handle the propagated errors correctly.

# Test: Search for the function usage. Expect: Proper error handling.
rg --type rust $'compile'

Length of output: 69084


Script:

#!/bin/bash
# Description: Verify all function calls to `compile` in `crates/dojo-lang/src/compiler.rs` handle the propagated errors correctly.

# Search for the `compile` function definition and calls within the `dojo-lang` crate.
ast-grep --lang rust --pattern $'fn compile($_, $_, $_) -> $_ {
  $$$
}' > compile_definition.txt

# Extract the function name and its usage in the crate.
grep -E 'fn compile' compile_definition.txt

# Search for the function calls to `compile` and ensure proper error handling.
ast-grep --lang rust --pattern $'compile($_, $_, $_)?' crates/dojo-lang/
ast-grep --lang rust --pattern $'compile($_, $_, $_).unwrap()' crates/dojo-lang/
ast-grep --lang rust --pattern $'compile($_, $_, $_).expect($$)' crates/dojo-lang/

Length of output: 359


Script:

#!/bin/bash
# Description: Re-verify all function calls to `compile` in the codebase and ensure proper error handling.

# Search for the `compile` function definition within the `dojo-lang` crate.
ast-grep --lang rust --pattern $'fn compile($_, $_, $_) -> $_ {
  $$$
}' > compile_definition.txt

# Extract the function name and its usage in the crate.
grep -E 'fn compile' compile_definition.txt

# Search for the function calls to `compile` and ensure proper error handling across all crates.
ast-grep --lang rust --pattern $'compile($_, $_, $_)?' > compile_calls_handled.txt
ast-grep --lang rust --pattern $'compile($_, $_, $_).unwrap()' > compile_calls_unwrap.txt
ast-grep --lang rust --pattern $'compile($_, $_, $_).expect($$)' > compile_calls_expect.txt

# Output the results for review.
cat compile_calls_handled.txt
cat compile_calls_unwrap.txt
cat compile_calls_expect.txt

Length of output: 399


Script:

#!/bin/bash
# Description: Re-verify all function calls to `compile` in the codebase and ensure proper error handling using `rg`.

# Search for the `compile` function definition within the `dojo-lang` crate.
rg --type rust --pcre2 --context 5 'fn compile\(' > compile_definition.txt

# Search for the function calls to `compile` across all crates and ensure proper error handling.
rg --type rust --pcre2 'compile\(' > compile_calls.txt

# Output the results for review.
cat compile_definition.txt
cat compile_calls.txt

Length of output: 2308

@@ -47,7 +47,7 @@ impl CallArgs {
self.tag_or_address
} else {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let default_namespace = get_default_namespace_from_ws(&ws);
let default_namespace = get_default_namespace_from_ws(&ws)?;
Copy link

Choose a reason for hiding this comment

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

LGTM! Consider adding error context for better debugging.

The addition of the ? operator ensures that errors are propagated correctly. However, adding context to the error can help with debugging.

let default_namespace = get_default_namespace_from_ws(&ws).context("Failed to get default namespace from workspace")?;

@@ -56,7 +56,7 @@ impl ExecuteArgs {
self.tag_or_address
} else {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
let default_namespace = get_default_namespace_from_ws(&ws);
let default_namespace = get_default_namespace_from_ws(&ws)?;
Copy link

Choose a reason for hiding this comment

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

LGTM! Consider adding error context for better debugging.

The addition of the ? operator ensures that errors are propagated correctly. However, adding context to the error can help with debugging.

let default_namespace = get_default_namespace_from_ws(&ws).context("Failed to get default namespace from workspace")?;

@@ -61,7 +61,7 @@ pub fn setup_migration(config: &Config) -> Result<MigrationStrategy> {
let base_dir = manifest_path.parent().unwrap();
let target_dir = format!("{}/target/dev", base_dir);

let default_namespace = get_default_namespace_from_ws(&ws);
let default_namespace = get_default_namespace_from_ws(&ws).unwrap();
Copy link

Choose a reason for hiding this comment

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

Consider adding a meaningful error message.

Using .unwrap() in test setups is generally acceptable, but providing a meaningful error message can make debugging easier.

let default_namespace = get_default_namespace_from_ws(&ws).expect("Failed to get default namespace from workspace");

@@ -50,7 +50,7 @@ async fn test_entities_queries() {

let target_path = ws.target_dir().path_existent().unwrap().join(config.profile().to_string());

let default_namespace = get_default_namespace_from_ws(&ws);
let default_namespace = get_default_namespace_from_ws(&ws).unwrap();
Copy link

Choose a reason for hiding this comment

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

Consider adding a meaningful error message.

Using .unwrap() in tests is generally acceptable, but providing a meaningful error message can make debugging easier.

let default_namespace = get_default_namespace_from_ws(&ws).expect("Failed to get default namespace from workspace");

Copy link

codecov bot commented Jul 7, 2024

Codecov Report

Attention: Patch coverage is 61.11111% with 14 lines in your changes missing coverage. Please review.

Project coverage is 67.88%. Comparing base (ed9f0ce) to head (aac2854).

Files Patch % Lines
bin/sozo/src/commands/build.rs 53.33% 7 Missing ⚠️
crates/dojo-world/src/metadata.rs 76.92% 3 Missing ⚠️
bin/sozo/src/commands/auth.rs 0.00% 1 Missing ⚠️
bin/sozo/src/commands/call.rs 0.00% 1 Missing ⚠️
bin/sozo/src/commands/execute.rs 0.00% 1 Missing ⚠️
bin/sozo/src/commands/model.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2149      +/-   ##
==========================================
- Coverage   67.89%   67.88%   -0.02%     
==========================================
  Files         336      336              
  Lines       43086    43094       +8     
==========================================
+ Hits        29255    29256       +1     
- Misses      13831    13838       +7     

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

@glihm glihm merged commit 28d19df into dojoengine:main Jul 7, 2024
14 of 15 checks passed
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