Replace unwraps with expects in bevy_app, bevy_audio, bevy_core, and bevy_core_pipeline#3913
Replace unwraps with expects in bevy_app, bevy_audio, bevy_core, and bevy_core_pipeline#3913tyleranton wants to merge 12 commits into
Conversation
Ixentus
left a comment
There was a problem hiding this comment.
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.
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
Sourcewithout having it set up first like inbevy/crates/bevy_audio/src/lib.rs
Lines 57 to 59 in 0ccb9dd
It could be helpful to explain how to fix the issue
There was a problem hiding this comment.
Would this be a case where adding to the error codes is best for an explanation?
ghost
left a comment
There was a problem hiding this comment.
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
| 0, | ||
| *world | ||
| .get_resource::<Count>() | ||
| .expect("Could not find `Count` in the `World`.") |
There was a problem hiding this comment.
| .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.
| .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.
| .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.
| .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.
| .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`."); |
There was a problem hiding this comment.
| .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.
| .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.
| .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 <KDecayMusic@protonmail.com>
Co-authored-by: KDecay <KDecayMusic@protonmail.com>
|
@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 |
| MainPass3dNode::IN_VIEW, | ||
| ) | ||
| .unwrap(); | ||
| .expect("Could not add slot edge to `RenderGraph`."); |
There was a problem hiding this comment.
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
unwrapwithexpect#3899Solution
unwraps withexpects.Meta
This is my first time contributing and really looking at the code base, so I wasn't completely confident in all of my
expecterror messages. Feedback is welcome and appreciated!