Skip to content

Commit d9b31b8

Browse files
committed
fix: standardize body stream error reporting
1 parent 2b8c528 commit d9b31b8

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

actix-http/examples/h2c-detect.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use std::{convert::Infallible, io};
1010

11-
use actix_http::{HttpService, Request, Response, StatusCode};
11+
use actix_http::{body::BodyStream, HttpService, Request, Response, StatusCode};
1212
use actix_server::Server;
1313

1414
#[tokio::main(flavor = "current_thread")]
@@ -19,7 +19,12 @@ async fn main() -> io::Result<()> {
1919
.bind("h2c-detect", ("127.0.0.1", 8080), || {
2020
HttpService::build()
2121
.finish(|_req: Request| async move {
22-
Ok::<_, Infallible>(Response::build(StatusCode::OK).body("Hello!"))
22+
Ok::<_, Infallible>(Response::build(StatusCode::OK).body(BodyStream::new(
23+
futures_util::stream::iter([
24+
Ok::<_, String>("123".into()),
25+
Err("wertyuikmnbvcxdfty6t".to_owned()),
26+
]),
27+
)))
2328
})
2429
.tcp_auto_h2c()
2530
})?

actix-http/src/h1/dispatcher.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,10 @@ where
512512
}
513513

514514
Poll::Ready(Some(Err(err))) => {
515+
let err = err.into();
516+
tracing::error!("Response payload stream error: {err:?}");
515517
this.flags.insert(Flags::FINISHED);
516-
return Err(DispatchError::Body(err.into()));
518+
return Err(DispatchError::Body(err));
517519
}
518520

519521
Poll::Pending => return Ok(PollResponse::DoNothing),
@@ -549,6 +551,7 @@ where
549551
}
550552

551553
Poll::Ready(Some(Err(err))) => {
554+
tracing::error!("Response payload stream error: {err:?}");
552555
this.flags.insert(Flags::FINISHED);
553556
return Err(DispatchError::Body(
554557
Error::new_body().with_cause(err).into(),

actix-http/src/h2/dispatcher.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
future::Future,
55
marker::PhantomData,
66
net,
7-
pin::Pin,
7+
pin::{pin, Pin},
88
rc::Rc,
99
task::{Context, Poll},
1010
};
@@ -20,7 +20,6 @@ use h2::{
2020
Ping, PingPong,
2121
};
2222
use pin_project_lite::pin_project;
23-
use tracing::{error, trace, warn};
2423

2524
use crate::{
2625
body::{BodySize, BoxBody, MessageBody},
@@ -147,11 +146,13 @@ where
147146
if let Err(err) = res {
148147
match err {
149148
DispatchError::SendResponse(err) => {
150-
trace!("Error sending HTTP/2 response: {:?}", err)
149+
tracing::trace!("Error sending response: {err:?}");
150+
}
151+
DispatchError::SendData(err) => {
152+
tracing::warn!("Send data error: {err:?}");
151153
}
152-
DispatchError::SendData(err) => warn!("{:?}", err),
153154
DispatchError::ResponseBody(err) => {
154-
error!("Response payload stream error: {:?}", err)
155+
tracing::error!("Response payload stream error: {err:?}");
155156
}
156157
}
157158
}
@@ -228,9 +229,9 @@ where
228229
return Ok(());
229230
}
230231

231-
// poll response body and send chunks to client
232-
actix_rt::pin!(body);
232+
let mut body = pin!(body);
233233

234+
// poll response body and send chunks to client
234235
while let Some(res) = poll_fn(|cx| body.as_mut().poll_next(cx)).await {
235236
let mut chunk = res.map_err(|err| DispatchError::ResponseBody(err.into()))?;
236237

0 commit comments

Comments
 (0)