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

Show source by default, add feature to hide it #60

Merged
merged 4 commits into from
Oct 23, 2024

Conversation

schneems
Copy link
Contributor

close #59

Changes the default behavior so that the output in the readme now matches the output a user would see when they use the library. Specifically, it now includes the original std::io::Error in the output.

It introduces a feature anyhow. By default:

  • By default: fs-err will include std::io::Error in the Display output, and return None from Error::source()
  • With anyhow fs-err will not include std::io::Error in the Display output, and return Some(std::io::Error) from Error::source()

This is based on the guidance from #51. That discussion links to this 2020 discussion rust-lang/project-error-handling#23 that suggests that you should either print the source and return None from Error::source() (https://doc.rust-lang.org/std/error/trait.Error.html#method.source) or not print anything and return Some(E).

This allows users of anyhow (or similar) libraries to configure fs-err for the behavior they desire, while not hiding information by default from unsuspecting adopters that are not using those libraries. It optimizes for the case of accidentally showing extra information that a user can then investigate and disable, rather than hiding information that the user might not realize is missing.

close andrewhickman#59

Changes the default behavior so that the output in the readme now matches the output a user would see when they use the library. Specifically, it now includes the original `std::io::Error` in the output.

It introduces a feature `anyhow`. By default:

- By default: fs-err will include `std::io::Error` in the Display output, and return `None` from `Error::source()`
- With `anyhow` fs-err will not include std::io::Error in the Display output, and return `Some(std::io::Error)` from `Error::source()`

This is based on the guidance from andrewhickman#51. That discussion links to this 2020 discussion rust-lang/project-error-handling#23 that suggests that you should either print the source and return `None` from `Error::source()` (https://doc.rust-lang.org/std/error/trait.Error.html#method.source) or not print anything and return `Some(E)`.

This allows users of anyhow (or similar) libraries to configure fs-err for the behavior they desire, while not hiding information by default from unsuspecting adopters that are not using those libraries. It optimizes for the case of accidentally showing extra information that a user can then investigate and disable, rather than hiding information that the user might not realize is missing.
I discussed the logic here andrewhickman#59 (comment).

The prior suggested name "anyhow" was a little too specific/esoteric and in the event that the feature flag outlives the popularity of `anyhow` it wouldn't make sense.

This suggested name tries to focus on the behavior outcome of the feature. It empowers callers to format the "called by" information however they want by:

- Providing a source of that data (Error::source)
- Not formatting emitting that data in `Display`
@schneems schneems force-pushed the schneems/un-hide-error branch from 45d3b10 to 7c1df0a Compare October 18, 2024 17:45
@schneems schneems marked this pull request as ready for review October 18, 2024 17:47
@andrewhickman
Copy link
Owner

I'm leaning towards of expose_original_error for the feature flag name. I imagine it would mainly be used when you need direct access to the original std error for some reason. Users who only want to print the error shouldn't need to care about whether it is enabled or not.

At a glance it's not clear why that logic exists based solely on the feature name. Adding a doc here clarifies the intent.
@schneems
Copy link
Contributor Author

I think that's a pragmatic choice. I updated the PR with that wording. My only concern would be someone mistaking the intent when developing internally. To that end, I added a comment explaining it inside of the Display implementations. I don't think it's strictly required if you don't like it. Commits are small and layered on top of prior work.

Copy link
Owner

@andrewhickman andrewhickman left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

}?;

#[cfg(not(feature = "custom_caused_by"))]
write!(formatter, " caused by: {}", self.source)?;
Copy link
Owner

Choose a reason for hiding this comment

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

I think I prefer just seperating the original error with a colon, i.e.:

failed to open file `notfound`: The system cannot find the file specified. (os error 2)

to avoid confusion in case this gets printed as part of an anyhow error message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. I didn't see this comment until after it was merged. I've been checking the PR. I see it was made 4 days ago, maybe it was in a pending review state or maybe github had a hiccup?

Either way. I also see you merged it and made the change yourself, which is perfectly fine with me. Thanks again

@andrewhickman andrewhickman merged commit df8f93a into andrewhickman:main Oct 23, 2024
6 checks passed
andrewhickman added a commit that referenced this pull request Oct 23, 2024
andrewhickman added a commit that referenced this pull request Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document the need to handle the underlying error source explicitly
2 participants