Skip to content

Commit

Permalink
feat(error): change Display for Error to only print top error
Browse files Browse the repository at this point in the history
hyper's `Error` used to print the error source automatically, preferring
to provide a better default for users who do not know about `Report`.
But, to fit better with the wider ecosystem, this changes the format to
only print the hyper `Error` itself, and not its source.

Closes #2844

BREAKING CHANGE: The format no longer prints the error chain. Be sure to
  check if you are logging errors directly.

  The `Error::message()` method is removed, it is no longer needed.

  The `Error::into_cause()` method is removed.
  • Loading branch information
seanmonstar committed Sep 11, 2023
1 parent 7f0c7ba commit a18ba57
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ pub type Result<T> = std::result::Result<T, Error>;
type Cause = Box<dyn StdError + Send + Sync>;

/// Represents errors that can occur handling HTTP streams.
///
/// # Formatting
///
/// The `Display` implementation of this type will only print the details of
/// this level of error, even though it may have been caused by another error
/// and contain that error in its source. To print all the relevant
/// information, including the source chain, using something like
/// `std::error::Report`, or equivalent 3rd party types.
///
/// The contents of the formatted error message of this specific `Error` type
/// is unspecified. **You must not depend on it.** The wording and details may
/// change in any version, with the goal of improving error messages.
pub struct Error {
inner: Box<ErrorImpl>,
}
Expand Down Expand Up @@ -173,11 +185,6 @@ impl Error {
self.find_source::<TimedOut>().is_some()
}

/// Consumes the error, returning its cause.
pub fn into_cause(self) -> Option<Box<dyn StdError + Send + Sync>> {
self.inner.cause
}

pub(super) fn new(kind: Kind) -> Error {
Error {
inner: Box::new(ErrorImpl { kind, cause: None }),
Expand Down Expand Up @@ -332,11 +339,6 @@ impl Error {
}
}

/// The error's standalone message, without the message from the source.
pub fn message(&self) -> impl fmt::Display + '_ {
self.description()
}

fn description(&self) -> &str {
match self.inner.kind {
Kind::Parse(Parse::Method) => "invalid HTTP method parsed",
Expand Down Expand Up @@ -420,11 +422,7 @@ impl fmt::Debug for Error {

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref cause) = self.inner.cause {
write!(f, "{}: {}", self.description(), cause)
} else {
f.write_str(self.description())
}
f.write_str(self.description())
}
}

Expand Down

0 comments on commit a18ba57

Please sign in to comment.