Skip to content

Commit 50f123a

Browse files
authoredSep 13, 2023
feat(error): change Display for Error to only print top error (#3312)
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.
1 parent 0891c9e commit 50f123a

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed
 

‎src/error.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ pub type Result<T> = std::result::Result<T, Error>;
88
type Cause = Box<dyn StdError + Send + Sync>;
99

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

173-
/// Consumes the error, returning its cause.
174-
pub fn into_cause(self) -> Option<Box<dyn StdError + Send + Sync>> {
175-
self.inner.cause
176-
}
177-
178185
pub(super) fn new(kind: Kind) -> Error {
179186
Error {
180187
inner: Box::new(ErrorImpl { kind, cause: None }),
@@ -324,11 +331,6 @@ impl Error {
324331
}
325332
}
326333

327-
/// The error's standalone message, without the message from the source.
328-
pub fn message(&self) -> impl fmt::Display + '_ {
329-
self.description()
330-
}
331-
332334
fn description(&self) -> &str {
333335
match self.inner.kind {
334336
Kind::Parse(Parse::Method) => "invalid HTTP method parsed",
@@ -410,11 +412,7 @@ impl fmt::Debug for Error {
410412

411413
impl fmt::Display for Error {
412414
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
413-
if let Some(ref cause) = self.inner.cause {
414-
write!(f, "{}: {}", self.description(), cause)
415-
} else {
416-
f.write_str(self.description())
417-
}
415+
f.write_str(self.description())
418416
}
419417
}
420418

‎tests/client.rs

-4
Original file line numberDiff line numberDiff line change
@@ -2318,10 +2318,6 @@ mod conn {
23182318
let error = client.send_request(req).await.unwrap_err();
23192319

23202320
assert!(error.is_user());
2321-
assert_eq!(
2322-
error.to_string(),
2323-
"dispatch task is gone: user code panicked"
2324-
);
23252321
}
23262322

23272323
async fn drain_til_eof<T: tokio::io::AsyncRead + Unpin>(mut sock: T) -> io::Result<()> {

‎tests/server.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ fn post_with_chunked_body() {
523523

524524
#[test]
525525
fn post_with_chunked_overflow() {
526+
use std::error::Error as _;
526527
let server = serve();
527528
let mut req = connect(server.addr());
528529
req.write_all(
@@ -542,7 +543,7 @@ fn post_with_chunked_overflow() {
542543
.unwrap();
543544
req.read(&mut [0; 256]).unwrap();
544545

545-
let err = server.body_err().to_string();
546+
let err = server.body_err().source().unwrap().to_string();
546547
assert!(
547548
err.contains("overflow"),
548549
"error should be overflow: {:?}",

0 commit comments

Comments
 (0)
Please sign in to comment.