make Response::message_body fallible

This commit is contained in:
Rob Ede 2021-05-07 18:10:26 +01:00
parent 62890c831b
commit ab3452dee1
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 11 additions and 9 deletions

View File

@ -235,25 +235,27 @@ impl ResponseBuilder {
/// This `ResponseBuilder` will be left in a useless state.
#[inline]
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response<Body> {
self.message_body(body.into())
match self.message_body(body.into()) {
Ok(res) => res,
Err(err) => Response::from_error(err),
}
}
/// Generate response with a body.
///
/// This `ResponseBuilder` will be left in a useless state.
pub fn message_body<B>(&mut self, body: B) -> Response<B> {
// TODO: put error handling back somehow
// if let Some(e) = self.err.take() {
// return Response::from(Error::from(e)).into_body();
// }
pub fn message_body<B>(&mut self, body: B) -> Result<Response<B>, Error> {
if let Some(err) = self.err.take() {
return Err(err.into());
}
let response = self.head.take().expect("cannot reuse response builder");
Response {
Ok(Response {
head: response,
body: Some(body),
error: None,
}
})
}
/// Generate response with a streaming body.

View File

@ -52,7 +52,7 @@ where
fn call(&self, (req, mut framed): (Request, Framed<T, h1::Codec>)) -> Self::Future {
let fut = async move {
let res = ws::handshake(req.head()).unwrap().message_body(());
let res = ws::handshake(req.head()).unwrap().message_body(()).unwrap();
framed
.send((res, body::BodySize::None).into())