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 63dd0fc commit 45d3b10
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ failed to open file `does not exist.txt`
caused by: The system cannot find the file specified. (os error 2)
```

> 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

fs-err's API is the same as [`std::fs`][std::fs], so migrating code to use it is easy.
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

0 comments on commit 45d3b10

Please sign in to comment.