Skip to content

Commit 740654e

Browse files
committed
refactor(error): improve error message when user body ends early
1 parent 1e9cd4f commit 740654e

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/error.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ pub(super) enum Kind {
5353
/// Error while writing a body to connection.
5454
#[cfg(any(feature = "http1", feature = "http2"))]
5555
BodyWrite,
56-
/// The body write was aborted.
57-
BodyWriteAborted,
5856
/// Error calling AsyncWrite::shutdown()
5957
#[cfg(feature = "http1")]
6058
Shutdown,
@@ -96,6 +94,8 @@ pub(super) enum User {
9694
/// Error calling user's HttpBody::poll_data().
9795
#[cfg(any(feature = "http1", feature = "http2"))]
9896
Body,
97+
/// The user aborted writing of the outgoing body.
98+
BodyWriteAborted,
9999
/// Error calling user's MakeService.
100100
#[cfg(any(feature = "http1", feature = "http2"))]
101101
#[cfg(feature = "server")]
@@ -193,7 +193,7 @@ impl Error {
193193

194194
/// Returns true if the body write was aborted.
195195
pub fn is_body_write_aborted(&self) -> bool {
196-
matches!(self.inner.kind, Kind::BodyWriteAborted)
196+
matches!(self.inner.kind, Kind::User(User::BodyWriteAborted))
197197
}
198198

199199
/// Returns true if the error was caused by a timeout.
@@ -305,7 +305,7 @@ impl Error {
305305
}
306306

307307
pub(super) fn new_body_write_aborted() -> Error {
308-
Error::new(Kind::BodyWriteAborted)
308+
Error::new(Kind::User(User::BodyWriteAborted))
309309
}
310310

311311
fn new_user(user: User) -> Error {
@@ -444,7 +444,6 @@ impl Error {
444444
Kind::Body => "error reading a body from connection",
445445
#[cfg(any(feature = "http1", feature = "http2"))]
446446
Kind::BodyWrite => "error writing a body to connection",
447-
Kind::BodyWriteAborted => "body write aborted",
448447
#[cfg(feature = "http1")]
449448
Kind::Shutdown => "error shutting down connection",
450449
#[cfg(feature = "http2")]
@@ -454,6 +453,7 @@ impl Error {
454453

455454
#[cfg(any(feature = "http1", feature = "http2"))]
456455
Kind::User(User::Body) => "error from user's HttpBody stream",
456+
Kind::User(User::BodyWriteAborted) => "user body write aborted",
457457
#[cfg(any(feature = "http1", feature = "http2"))]
458458
#[cfg(feature = "server")]
459459
Kind::User(User::MakeService) => "error from user's MakeService",

src/proto/h1/conn.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,8 @@ where
686686
Writing::KeepAlive
687687
}
688688
}
689-
Err(_not_eof) => {
690-
res = Err(crate::Error::new_user_body(
691-
crate::Error::new_body_write_aborted(),
692-
));
689+
Err(not_eof) => {
690+
res = Err(crate::Error::new_body_write_aborted().with(not_eof));
693691
Writing::Closed
694692
}
695693
}

src/proto/h1/encode.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) struct EncodedBuf<B> {
2222
}
2323

2424
#[derive(Debug)]
25-
pub(crate) struct NotEof;
25+
pub(crate) struct NotEof(u64);
2626

2727
#[derive(Debug, PartialEq, Clone)]
2828
enum Kind {
@@ -98,7 +98,7 @@ impl Encoder {
9898
})),
9999
#[cfg(feature = "server")]
100100
Kind::CloseDelimited => Ok(None),
101-
Kind::Length(_) => Err(NotEof),
101+
Kind::Length(n) => Err(NotEof(n)),
102102
}
103103
}
104104

@@ -351,6 +351,14 @@ impl<B: Buf> From<Chain<Chain<ChunkSize, B>, StaticBuf>> for EncodedBuf<B> {
351351
}
352352
}
353353

354+
impl fmt::Display for NotEof {
355+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
356+
write!(f, "early end, expected {} more bytes", self.0)
357+
}
358+
}
359+
360+
impl std::error::Error for NotEof {}
361+
354362
#[cfg(test)]
355363
mod tests {
356364
use bytes::BufMut;

0 commit comments

Comments
 (0)