Skip to content

Commit

Permalink
Add section about not including sources in Display impl
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Jan 5, 2020
1 parent 7894a48 commit 8a708f9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/interoperability.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,27 @@ The message given by [`Error::description()`] does not matter. Users should
always use `Display` instead of `description()` to print the error. A low-effort
description like `"JSON error"` is sufficient.

If an error type returns an underlying source error from [`Error::source()`], it does **not**
include that source error in its own `Display` representation as well. This avoids duplicated
information when an error is printed along with all its sources:

```rust
use std::fmt;

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Good errors never duplicate information like this:
write!(f, "failed to parse: {}", self.source),

// Instead they just describe themselves:
write!(f, "failed to parse"),
}
}
```

[`Error::description()`]: https://doc.rust-lang.org/std/error/trait.Error.html#tymethod.description
[`Error::source()`]: https://doc.rust-lang.org/std/error/trait.Error.html#method.source


### Examples from the standard library

Expand Down

0 comments on commit 8a708f9

Please sign in to comment.