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

Better error reporting for embedded_path macro #11340

Closed

Conversation

shanecelis
Copy link
Contributor

Objective

Better error reporting for embedded_path macro.

A number of cases will cause a panic, which is fine. The issue is there are two or three different possible panics that all report an unwrap on a None value, so they're hard to differentiate for the user, and they were difficult for me to diagnose as a user.

Solution

This refactor does not change embedded_path!'s functional behavior. All cases that caused a panic before, will still cause a panic. However, this change will provide the user diagnostic information that will hopefully help them right whatever issue is causing the panic.

The original behavior that caught me up was this:

    #[test]
    #[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
    fn test_embedded_path_original_behavior() {
        embedded_path!("NOT-IN-PATH", "embed.png"),
    }

The changed behavior is this:

    #[test]
    #[should_panic(
        expected = "Expected source path `NOT-IN-PATH` in file path `crates/bevy_asset/src/io/embedded/mod.rs`"
    )]
    fn test_embedded_path_original_behavior() {
        embedded_path!("NOT-IN-PATH", "embed.png")
    }

Changelog

Added error reporting for embedded_path macro

A number of cases will panic, which is fine. The issue is there are two
or three different errors that all report an unwrap on a None value, so
they're hard to differentiate for the user.
This refactor does not change `embedded_path!`'s behavior. All cases
that caused a panic before, will still cause a panic. But this change
will provide the user diagnostic information that will hopefully help
them right whatever issue is causing the panic.

The original behavior was this:

```
    #[test]
    #[should_panic(expected = "called `Option::unwrap()` on a `None` value")]
    fn test_embedded_path_original_behavior() {
        embedded_path!("NOT-IN-PATH", "b"),
    }
```

The changed behavior is this:

```
    #[test]
    #[should_panic(
        expected = "Expected source path `NOT-IN-PATH` in file path `crates/bevy_asset/src/io/embedded/mod.rs`"
    )]
    fn test_embedded_path_original_behavior() {
        embedded_path!("NOT-IN-PATH", "b")
    }
```
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@matiqo15 matiqo15 added A-Assets Load files from disk to use for things like images, models, and sounds C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jan 14, 2024
@shanecelis
Copy link
Contributor Author

As I was fixing the tests to work on Windows, I noticed the "/src/" hardcoding for embedded paths isn't going to work as is. That appears to be fixed by #10383, so perhaps this PR shouldn't be considered until that PR is closed.

@matiqo15 matiqo15 added the S-Blocked This cannot move forward until something else changes label Jan 14, 2024
let crate_name = module_path!()
.split(':')
.next()
.expect("Could not find crate name");
Copy link
Member

Choose a reason for hiding this comment

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

Can we report the original string here for better diagnosability?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly. Is this the kind of error message you're looking for?

    panic!("Could not find crate name. Expected ':' in module path `{}`", module_path!());

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Looks good: always nice to improve error messages like this.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Blocked This cannot move forward until something else changes labels Jan 14, 2024
@shanecelis
Copy link
Contributor Author

In reviewing #10383, I'd suggest closing this PR and going with that one. It does what I wanted to do here and more.

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-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants