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

Extract an iterator that cleans redundant nested error text #383

Merged
merged 1 commit into from
Jul 6, 2023

Conversation

shepmaster
Copy link
Owner

This allows the cleaning logic to be reused in other contexts, such as an HTML or JSON error report.

@shepmaster shepmaster added enhancement New feature or request feedback requested User feedback desired on a design decision labels Jul 5, 2023
@netlify
Copy link

netlify bot commented Jul 5, 2023

Deploy Preview for shepmaster-snafu ready!

Name Link
🔨 Latest commit e4e57ea
🔍 Latest deploy log https://app.netlify.com/sites/shepmaster-snafu/deploys/64a6bb17a1ff0d00081682b5
😎 Deploy Preview https://deploy-preview-383--shepmaster-snafu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

src/report.rs Outdated Show resolved Hide resolved
@@ -162,3 +162,59 @@ struct TestFunctionError;
fn procedural_macro_works_with_test_functions() -> Result<(), TestFunctionError> {
Ok(())
}

#[track_caller]
fn assert_cleaning_step(iter: &mut CleanedErrorText, text: &str, removed_text: &str) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have test coverage for the case where a message contains :, but the subsequent part of the string represents an unreachable cause not exposed via source? No clean-up should happen in these cases.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Do you mean something like a leaf message saying “bad thing happened: restart your server”?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, or the case where the second part is indeed part of a sub-cause error message but the corresponding cause is deliberately not part of the error source chain.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm not sure I follow that second case. Do you mean something like this pseudocode?

#[snafu(display("Outer is better than Inner"))] // doesn't use `{source}` but just happens to end with the exact same text
struct Outer { source: Inner }

#[snafu(display("Inner"))]
struct Inner;

If so, I don't think there's any way to avoid that.

Copy link
Collaborator

@Enet4 Enet4 Jul 6, 2023

Choose a reason for hiding this comment

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

I was thinking about the code below. I suspect the confusion is because one is unlikely to do this in snafu, but if for any reason we have a source error which we do not want to expose as an error, the error text cleaning routine should not clean the message.

#[derive(Debug, Snafu)]
#[snafu(display("Epic fail: {}", hidden_source))]
struct Outer {
   hidden_source: Inner,
}

#[derive(Debug, Snafu)]
#[snafu(display("I AM ERROR"))]
struct Inner;

The display message of an Outer should be "Epic fail: I AM ERROR", per the recommendations of the error handling WG.


The situation you just mentioned is a curious one, though! Could we have problems with the outer error's message ending with the exact message as its source-error message?

Copy link
Owner Author

Choose a reason for hiding this comment

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

we do not want to expose as an error

In that case, it would not be cleaned — by construction. This walks the Error::source chain to find the duplicate text. It'e effectively trying to retroactively apply the error handling WG's suggestion of (Error::source XOR "include it in Display) for errors that do both.

Calling Error::source on Outer will return None, so we will never get the Display string from Inner, so we will not use it to clean Outer's text.


Could we have problems with the outer error's message ending with the exact message as its source-error message?

It's always possible. That's why snafu::Report offers a way to disable the cleaning. I do think that it is pretty unlikely, however.

This allows the cleaning logic to be reused in other contexts, such as an HTML
or JSON error report.
@shepmaster shepmaster force-pushed the cleaned-error-text branch from 07c3d45 to e4e57ea Compare July 6, 2023 13:01
Copy link
Collaborator

@Enet4 Enet4 left a comment

Choose a reason for hiding this comment

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

It's fine by me.

@shepmaster shepmaster merged commit ecd6c82 into main Jul 6, 2023
@shepmaster shepmaster deleted the cleaned-error-text branch July 6, 2023 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feedback requested User feedback desired on a design decision
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants