Skip to content

Commit 7c1df0a

Browse files
committed
Update feature name
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`
1 parent b2179c1 commit 7c1df0a

5 files changed

Lines changed: 18 additions & 15 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ jobs:
6868
args: --features tokio
6969
if: ${{ matrix.rust_version == 'stable' || matrix.rust_version == 'beta' }}
7070

71-
- name: cargo check --features anyhow
71+
- name: cargo check --features custom_caused_by
7272
uses: actions-rs/cargo@v1
7373
with:
7474
command: check
75-
args: --features anyhow
75+
args: --features custom_caused_by

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# fs-err Changelog
22

3-
* 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)).
3+
* 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)).
44

55
## 2.11.0
66

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ serde_json = "1.0.64"
2323
[features]
2424
# Adds I/O safety traits, introduced in Rust 1.63
2525
io_safety = []
26-
# Removes the original error text from Display and relies on a wrapper library
27-
# (such as anyhow) to emit them via `Error::source()`.
28-
anyhow = []
26+
27+
# Allow custom formatting of the error source
28+
#
29+
# When enabled errors emit `std::error::Error::source()` as Some (default is `None`) and
30+
# no longer include the original `std::io::Error` source in the `Display` implementation.
31+
# This is useful if errors are wrapped in another library such as Anyhow.
32+
custom_caused_by = []
2933

3034
[package.metadata.release]
3135
tag-name = "{{version}}"

src/errors.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl fmt::Display for Error {
101101
E::WriteAt => write!(formatter, "failed to write with offset to `{}`", path),
102102
}?;
103103

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

107107
Ok(())
@@ -113,12 +113,12 @@ impl StdError for Error {
113113
self.source()
114114
}
115115

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

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

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

207207
Ok(())
@@ -213,12 +213,12 @@ impl StdError for SourceDestError {
213213
self.source()
214214
}
215215

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

221-
#[cfg(feature = "anyhow")]
221+
#[cfg(feature = "custom_caused_by")]
222222
fn source(&self) -> Option<&(dyn StdError + 'static)> {
223223
Some(&self.source)
224224
}

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ failed to open file `does not exist.txt`
2626
caused by: The system cannot find the file specified. (os error 2)
2727
```
2828
29-
> Note: To bypass displaying the original error message you can enable the `anyhow` feature.
30-
> When the `anyhow` feature is enabled `Error::source()` will return `Some` and the original
31-
> error will not be `Display`-ed via fs-err.
29+
> 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.
30+
> 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.
3231
3332
# Usage
3433

0 commit comments

Comments
 (0)