Skip to content

Replace unwraps with expects in bevy_app, bevy_audio, bevy_core, and bevy_core_pipeline#3913

Closed
tyleranton wants to merge 12 commits into
bevyengine:mainfrom
tyleranton:expecting-unwraps
Closed

Replace unwraps with expects in bevy_app, bevy_audio, bevy_core, and bevy_core_pipeline#3913
tyleranton wants to merge 12 commits into
bevyengine:mainfrom
tyleranton:expecting-unwraps

Conversation

@tyleranton

@tyleranton tyleranton commented Feb 10, 2022

Copy link
Copy Markdown

Objective

Solution

  • Provide somewhat more helpful error messages by replacing unwraps with expects.

Meta

This is my first time contributing and really looking at the code base, so I wasn't completely confident in all of my expect error messages. Feedback is welcome and appreciated!

@github-actions github-actions Bot added the S-Needs-Triage This issue needs to be labelled label Feb 10, 2022

@Ixentus Ixentus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Just some nits.

Comment thread crates/bevy_core_pipeline/src/main_pass_2d.rs Outdated
Comment thread crates/bevy_core_pipeline/src/main_pass_driver.rs Outdated
Comment thread crates/bevy_core_pipeline/src/clear_pass.rs Outdated
Comment thread crates/bevy_core/src/time/fixed_timestep.rs Outdated
Comment thread crates/bevy_core/src/time/fixed_timestep.rs
@ghost ghost mentioned this pull request Feb 11, 2022
31 tasks
@IceSentry IceSentry added C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use and removed S-Needs-Triage This issue needs to be labelled labels Feb 11, 2022
@tyleranton

Copy link
Copy Markdown
Author

Is amending commits okay here since it's simple, or are new commits preferred? Thanks for taking time to review this!

@ghost

ghost commented Feb 11, 2022

Copy link
Copy Markdown

That shouldn't really matter. The fastest way to commit the suggested changes is to use the commit suggestion button directly below the suggestion. This will create new commits, which is the way I saw it being done most of the time. Feel free to use whatever method is more convenient for you. For bigger changes I would prefer a new commit though.

Comment thread crates/bevy_app/src/schedule_runner.rs Outdated
Comment thread crates/bevy_app/src/schedule_runner.rs Outdated
Comment thread crates/bevy_audio/src/audio_output.rs Outdated
Comment thread crates/bevy_audio/src/audio_output.rs Outdated
Comment on lines +78 to +83
let audio_output = world
.get_non_send::<AudioOutput<Source>>()
.expect("Could not find `AudioOutput` in the `World`.");
let mut audio = world
.get_resource_mut::<Audio<Source>>()
.expect("Could not find `Audio` in the `World`.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In both those cases, the issue would be either:

  • the user is calling this system without adding the AudioPlugin
  • the user is calling this system on a custom Source without having it set up first like in
    app.init_non_send_resource::<AudioOutput<AudioSource>>()
    .add_asset::<AudioSource>()
    .init_resource::<Audio<AudioSource>>()

It could be helpful to explain how to fix the issue

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Would this be a case where adding to the error codes is best for an explanation?

Comment thread crates/bevy_core/src/time/fixed_timestep.rs Outdated
Comment thread crates/bevy_core_pipeline/src/lib.rs Outdated
@tyleranton

Copy link
Copy Markdown
Author

Just wanted to bump this for further review
@mockersf @Ixentus @KDecay

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did a pass over everything. I think you might be missing some unwraps. For example in the bevy_core_pipeline there is an unwrap() that didn't get addressed. Could you do a second pass over the crates and see if you find any more unwraps that should be changed? Thank you <3

Comment thread crates/bevy_audio/src/audio_source.rs Outdated
Comment thread crates/bevy_core/src/time/fixed_timestep.rs Outdated
0,
*world
.get_resource::<Count>()
.expect("Could not find `Count` in the `World`.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not find `Count` in the `World`.")
.expect("Could not get the `Count` resource from the `World`")

1,
*world
.get_resource::<Count>()
.expect("Could not find `Count` in the `World`.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not find `Count` in the `World`.")
.expect("Could not get the `Count` resource from the `World`")

3,
*world
.get_resource::<Count>()
.expect("Could not find `Count` in the `World`.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not find `Count` in the `World`.")
.expect("Could not get the `Count` resource from the `World`")

let draw_functions = world.get_resource::<DrawFunctions<AlphaMask3d>>().unwrap();
let draw_functions = world
.get_resource::<DrawFunctions<AlphaMask3d>>()
.expect("Could not get `DrawFunctions` resource from the `World`.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not get `DrawFunctions` resource from the `World`.");
.expect("Could not get the `DrawFunctions<AlphaMask3d>` resource from the `World`. It can be added using the `CorePipelinePlugin`, or the `DefaultPlugins`");

let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
let draw_function = draw_functions
.get_mut(item.draw_function)
.expect("Could not get draw function.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not get draw function.");
.unwrap_or_else(|| {
panic!("Could not get the draw function for id: {}", item.draw_function.0);
});

let draw_functions = world
.get_resource::<DrawFunctions<Transparent3d>>()
.unwrap();
.expect("Could not get `DrawFunctions` resource from the `World`.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not get `DrawFunctions` resource from the `World`.");
.expect("Could not get the `DrawFunctions<Transparent3d>` resource from the `World`. It can be added using the `CorePipelinePlugin`, or the `DefaultPlugins`");

let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
let draw_function = draw_functions
.get_mut(item.draw_function)
.expect("Could not get draw function.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not get draw function.");
.unwrap_or_else(|| {
panic!("Could not get the draw function for id: {}", item.draw_function.0);
});

let extracted_cameras = world.get_resource::<ExtractedCameraNames>().unwrap();
let extracted_cameras = world
.get_resource::<ExtractedCameraNames>()
.expect("Could not get `ExtractedCameraNames` resource from the `World`.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
.expect("Could not get `ExtractedCameraNames` resource from the `World`.");
.expect("Could not get the `ExtractedCameraNames` resource from the `World`. It can be added using the `CameraPlugin`, `RenderPlugin` or the `DefaultPlugins`");

tyleranton and others added 2 commits February 26, 2022 19:31
Co-authored-by: KDecay <KDecayMusic@protonmail.com>
Co-authored-by: KDecay <KDecayMusic@protonmail.com>
@tyleranton

tyleranton commented Feb 26, 2022

Copy link
Copy Markdown
Author

@KDecay wow thanks for going through all of this. Would committing all of these suggestions separately be too noisy? I know you said it's common to use that feature, just wasn't sure if volume mattered here. Appreciate the review!

Edit: Just saw the request to pause work on the tracking issue, so you can disregard the above. Thanks again!

@ghost

ghost commented Feb 27, 2022

Copy link
Copy Markdown

@KDecay wow thanks for going through all of this. Would committing all of these suggestions separately be too noisy? I know you said it's common to use that feature, just wasn't sure if volume mattered here. Appreciate the review!

@tylerdotdev You can also combine the small suggestions into one larger commit to reduce noise.

Edit: Just saw the request to pause work on the tracking issue, so you can disregard the above. Thanks again!

Yup, we figured that it would be beneficial to get some of the extremely common cases out of the way first (e.g. failing on getting a resource). This will remove a lot of expects in all of the PR's related to the tracking issue which means that we can focus on the error messages themselves instead of painfully trying to keep everything consistent throughout all of the PR's.

MainPass3dNode::IN_VIEW,
)
.unwrap();
.expect("Could not add slot edge to `RenderGraph`.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Something to consider before throwing expects onto more "internal" non-user facing code: static strings are compiled into the binary. I don't think we want to bloat bevy apps with a bunch of "internal error messages", especially in cases like these where the context of the code is as good (if not better) for debugging than the message in the expect (for a Bevy Engine dev debugging a user-reported error).

@alice-i-cecile

Copy link
Copy Markdown
Member

Closing per discussion in #3899 <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Code-Quality A section of code that is hard to understand or change C-Usability A targeted quality-of-life change that makes Bevy easier to use

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants