-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Replace unwraps with expects in bevy_app, bevy_audio, bevy_core, and bevy_core_pipeline #3913
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
Conversation
There was a problem hiding this comment.
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.
Is amending commits okay here since it's simple, or are new commits preferred? Thanks for taking time to review this! |
That shouldn't really matter. The fastest way to commit the suggested changes is to use the |
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`."); |
There was a problem hiding this comment.
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 inbevy/crates/bevy_audio/src/lib.rs
Lines 57 to 59 in 0ccb9dd
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 unwrap
s. 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 unwrap
s that should be changed? Thank you <3
0, | ||
*world | ||
.get_resource::<Count>() | ||
.expect("Could not find `Count` in the `World`.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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`.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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`.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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`."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect("Could not get draw function."); | |
.unwrap_or_else(|| { | |
panic!("Could not get the draw function for id: {}", item.draw_function.0); | |
}); |
@@ -147,15 +153,17 @@ impl Node for MainPass3dNode { | |||
|
|||
let draw_functions = world | |||
.get_resource::<DrawFunctions<Transparent3d>>() | |||
.unwrap(); | |||
.expect("Could not get `DrawFunctions` resource from the `World`."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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`."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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`"); |
Co-authored-by: KDecay <[email protected]>
Co-authored-by: KDecay <[email protected]>
@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! |
@tylerdotdev You can also combine the small suggestions into one larger commit to reduce noise.
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 |
@@ -176,7 +176,7 @@ impl Plugin for CorePipelinePlugin { | |||
draw_3d_graph::node::MAIN_PASS, | |||
MainPass3dNode::IN_VIEW, | |||
) | |||
.unwrap(); | |||
.expect("Could not add slot edge to `RenderGraph`."); |
There was a problem hiding this comment.
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).
Closing per discussion in #3899 <3 |
Objective
unwrap
withexpect
#3899Solution
unwrap
s withexpect
s.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!