{"id":"ad6d3b0b0fa0e109","repo":"hyperium/hyper","slug":"error-reading-a-body-from-connection","errorCode":null,"errorMessage":"error reading a body from connection","messagePattern":"error reading a body from connection","errorType":"exception","errorClass":"hyper::Error","httpStatus":null,"severity":"error","filePath":"src/error.rs","lineNumber":406,"sourceCode":"    ))]\n    pub(super) fn new_io(cause: std::io::Error) -> Error {\n        Error::new(Kind::Io).with(cause)\n    }\n\n    #[cfg(any(\n        all(feature = \"http1\", any(feature = \"client\", feature = \"server\")),\n        all(feature = \"http2\", feature = \"client\")\n    ))]\n    pub(super) fn new_closed() -> Error {\n        Error::new(Kind::ChannelClosed)\n    }\n\n    #[cfg(all(\n        any(feature = \"client\", feature = \"server\"),\n        any(feature = \"http1\", feature = \"http2\")\n    ))]\n    pub(super) fn new_body<E: Into<Cause>>(cause: E) -> Error {\n        Error::new(Kind::Body).with(cause)\n    }\n\n    #[cfg(all(\n        any(feature = \"client\", feature = \"server\"),\n        any(feature = \"http1\", feature = \"http2\")\n    ))]\n    pub(super) fn new_body_write<E: Into<Cause>>(cause: E) -> Error {\n        Error::new(Kind::BodyWrite).with(cause)\n    }\n\n    #[cfg(any(\n        all(feature = \"http1\", any(feature = \"client\", feature = \"server\")),\n        feature = \"ffi\"\n    ))]\n    pub(super) fn new_body_write_aborted() -> Error {\n        Error::new(Kind::User(User::BodyWriteAborted))\n    }\n","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/hyperium/hyper/blob/084473f728f9d07b3be5845475aa2f62ed9ff579/src/error.rs#L388-L424","documentation":"Thrown via Error::new_body() (src/error.rs:406, Kind::Body). It means reading the message body from the connection failed — an IO or framing error occurred while streaming body bytes. The concrete cause is attached via .with(cause). Notable producer: proto/h1/dispatch.rs:132 sends new_body(\"connection error\") into the body when the connection errors during body streaming, and dispatch.rs:280 forwards body errors.","triggerScenarios":"The socket returns an error (EOF/RST) partway through reading a chunked or Content-Length body; the dispatch loop detects a connection error while a body frame is outstanding (dispatch.rs:132); an HTTP/2 body stream errors (DATA frame / stream error). The reader of hyper::body sees it as a body read failure.","commonSituations":"Upstream cuts the body short (length mismatch / premature close); a proxy times out and drops the body; network glitch mid-download; chunked encoding truncated; a server that closes before sending the promised Content-Length bytes.","solutions":["Inspect Error::source() for the underlying io::Error or h2::Error to distinguish truncation from a hard network failure.","For idempotent requests, retry the whole request on a new connection (partial bodies must be discarded).","If you are a server, log the request id and 4xx/5xx appropriately; ensure clients send accurate Content-Length or well-formed chunked bodies."],"exampleFix":"// before: read whole body, any failure is fatal\nlet bytes = hyper::body::to_bytes(resp.into_body()).await?;\n\n// after: surface truncation distinctly and retry idempotent fetches\nasync fn read_all(client: &Client<...>, uri: Uri) -> Result<Bytes, hyper::Error> {\n    for attempt in 0..2u8 {\n        let resp = client.get(uri.clone()).await?;\n        match hyper::body::to_bytes(resp.into_body()).await {\n            Ok(b) => return Ok(b),\n            Err(e) if attempt == 0 => continue, // body read failed, retry\n            Err(e) => return Err(e),\n        }\n    }\n    unreachable!()\n}","handlingStrategy":"retry","validationCode":null,"typeGuard":"fn is_body_read_error(err: &hyper::Error) -> bool {\n    // hyper doesn't expose a dedicated predicate for Kind::Body; classify via source.\n    err.source().and_then(|s| s.downcast_ref::<std::io::Error>()).is_some()\n}","tryCatchPattern":"for attempt in 0..2u8 {\n    let resp = client.get(uri.clone()).await?;\n    match hyper::body::to_bytes(resp.into_body()).await {\n        Ok(b) => return Ok(b),\n        Err(e) if attempt == 0 && idempotent => continue,\n        Err(e) => return Err(e),\n    }\n}","preventionTips":["For idempotent requests, retry the whole request (discard partial bytes) when the body read fails.","Inspect the source io::Error/h2::Error to distinguish truncation from a hard failure.","Producers should advertise accurate Content-Length or well-formed chunked encoding."],"tags":["body","network","http1","http2","rust"],"analyzedSha":"084473f728f9d07b3be5845475aa2f62ed9ff579","analyzedAt":"2026-08-06T01:20:18.522Z","schemaVersion":2}