Skip to content

Replace unwraps with expects in bevy_asset #3892

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

Closed

Conversation

alice-i-cecile
Copy link
Member

@alice-i-cecile alice-i-cecile commented Feb 8, 2022

Objective

  • Panics in bevy_asset are often particularly frustrating and obtuse.

Solution

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

Meta

This is a very basic, mechanical pass to improve error handling.

I don't currently have a deep understanding of the internals (and they're likely to change), so the error messages provided are very localized. It would be nice to capture more information about the state in these expect messages as well before merging.

There are a few wasm-specific unwraps left in wasm_asset_io.rs that I didn't feel comfortable tackling. There are also some expects in test code left, which I felt were fine to keep.

Like always, improvements are very welcome.

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Feb 8, 2022
@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds 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 8, 2022
Copy link
Contributor

@rparrett rparrett left a comment

Choose a reason for hiding this comment

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

Not informed enough to comment on a lot of these. But it seems like in many cases there's some type of Error being ignored that likely contains some useful information about what went wrong.

@@ -358,7 +362,7 @@ impl AssetServer {
self.server
.asset_io
.watch_path_for_changes(asset_path.path())
.unwrap();
.expect("Could not watch path for changes.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this one could be .watch_path_for_changes(asset_path.path())?;

@@ -399,7 +403,9 @@ impl AssetServer {
let path = path.as_ref();
if !self.server.asset_io.is_directory(path) {
return Err(AssetServerError::AssetFolderNotADirectory(
path.to_str().unwrap().to_string(),
path.to_str()
.expect("Could not convert path to string.")
Copy link
Contributor

@rparrett rparrett Feb 8, 2022

Choose a reason for hiding this comment

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

Seems like this should only fail if the path has invalid utf8, so the message could be made more specific.

@alice-i-cecile
Copy link
Member Author

But it seems like in many cases there's some type of Error being ignored that likely contains some useful information about what went wrong.

Yep; that's my next pass for this PR.

@alice-i-cecile alice-i-cecile marked this pull request as draft February 8, 2022 16:11
Co-authored-by: Charles <[email protected]>
@IceSentry
Copy link
Contributor

Sorry, my mistake, the method is unwrap_or_else() not expect_or_else. My local setup is giving me some issues and I didn't notice the mistake.

Co-authored-by: Charles <[email protected]>
@alice-i-cecile
Copy link
Member Author

Closing per discussion in #3899.

bors bot pushed a commit that referenced this pull request May 2, 2022
# Objective

- Manually running systems is a somewhat obscure process: systems must be initialized before they are run
- The unwrap is rather hard to debug.

## Solution

- Replace unwraps in `FunctionSystem` methods with expects (progress towards #3892).
- Briefly document this requirement.
exjam pushed a commit to exjam/bevy that referenced this pull request May 22, 2022
…ine#3947)

# Objective

- Manually running systems is a somewhat obscure process: systems must be initialized before they are run
- The unwrap is rather hard to debug.

## Solution

- Replace unwraps in `FunctionSystem` methods with expects (progress towards bevyengine#3892).
- Briefly document this requirement.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
…ine#3947)

# Objective

- Manually running systems is a somewhat obscure process: systems must be initialized before they are run
- The unwrap is rather hard to debug.

## Solution

- Replace unwraps in `FunctionSystem` methods with expects (progress towards bevyengine#3892).
- Briefly document this requirement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds 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.

3 participants