Skip to content

Commit

Permalink
Update feature name
Browse files Browse the repository at this point in the history
I discussed the logic here #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`
  • Loading branch information
schneems committed Oct 18, 2024
1 parent b2179c1 commit 7c1df0a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ jobs:
args: --features tokio
if: ${{ matrix.rust_version == 'stable' || matrix.rust_version == 'beta' }}

- name: cargo check --features anyhow
- name: cargo check --features custom_caused_by
uses: actions-rs/cargo@v1
with:
command: check
args: --features anyhow
args: --features custom_caused_by
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# fs-err Changelog

* Change errors to output original `std::io::Error` information Display by default. This functionality can be disabled for [anyhow](https://docs.rs/anyhow/latest/anyhow/) users by using the new feature `anyhow` ([#60](https://github.com/andrewhickman/fs-err/pull/60)).
* Change errors to output original `std::io::Error` information Display by default. This functionality can be disabled for [anyhow](https://docs.rs/anyhow/latest/anyhow/) users by using the new feature `custom_caused_by` ([#60](https://github.com/andrewhickman/fs-err/pull/60)).

## 2.11.0

Expand Down
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ serde_json = "1.0.64"
[features]
# Adds I/O safety traits, introduced in Rust 1.63
io_safety = []
# Removes the original error text from Display and relies on a wrapper library
# (such as anyhow) to emit them via `Error::source()`.
anyhow = []

# Allow custom formatting of the error source
#
# When enabled errors emit `std::error::Error::source()` as Some (default is `None`) and
# no longer include the original `std::io::Error` source in the `Display` implementation.
# This is useful if errors are wrapped in another library such as Anyhow.
custom_caused_by = []

[package.metadata.release]
tag-name = "{{version}}"
Expand Down
12 changes: 6 additions & 6 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl fmt::Display for Error {
E::WriteAt => write!(formatter, "failed to write with offset to `{}`", path),
}?;

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

Ok(())
Expand All @@ -113,12 +113,12 @@ impl StdError for Error {
self.source()
}

#[cfg(not(feature = "anyhow"))]
#[cfg(not(feature = "custom_caused_by"))]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
None
}

#[cfg(feature = "anyhow")]
#[cfg(feature = "custom_caused_by")]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.source)
}
Expand Down Expand Up @@ -201,7 +201,7 @@ impl fmt::Display for SourceDestError {
}
}?;

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

Ok(())
Expand All @@ -213,12 +213,12 @@ impl StdError for SourceDestError {
self.source()
}

#[cfg(not(feature = "anyhow"))]
#[cfg(not(feature = "custom_caused_by"))]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
None
}

#[cfg(feature = "anyhow")]
#[cfg(feature = "custom_caused_by")]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.source)
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ failed to open file `does not exist.txt`
caused by: The system cannot find the file specified. (os error 2)
```
> Note: To bypass displaying the original error message you can enable the `anyhow` feature.
> When the `anyhow` feature is enabled `Error::source()` will return `Some` and the original
> error will not be `Display`-ed via fs-err.
> Note: Users of `anyhow` or other libraries that format an Error's sources can enable the `custom_caused_by` feature to control the formatting of the orginal error message.
> When enabled, the `std::fmt::Display` implementation will emit the failed operation and paths but not the original `std::io::Error`. It will instead provide access via [Error::source](https://doc.rust-lang.org/std/error/trait.Error.html#method.source), which will be used by `anyhow` (or similar) libraries.
# Usage
Expand Down

0 comments on commit 7c1df0a

Please sign in to comment.