diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index ef57d598c5d4..7be846aeeb14 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -398,6 +398,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fixed excessive memory usage introduced in 7.5 due to over-allocating memory for HTTP checks. {pull}15639[15639] - Fixed TCP TLS checks to properly validate hostnames, this broke in 7.x and only worked for IP SANs. {pull}17549[17549] +- Add Context to otherwise ambiguous HTTP body read errors. {pull}25499[25499] *Journalbeat* diff --git a/heartbeat/monitors/active/http/respbody.go b/heartbeat/monitors/active/http/respbody.go index 54c0bd9b63e9..0587c207be3b 100644 --- a/heartbeat/monitors/active/http/respbody.go +++ b/heartbeat/monitors/active/http/respbody.go @@ -20,6 +20,7 @@ package http import ( "crypto/sha256" "encoding/hex" + "fmt" "io" "net/http" "strings" @@ -58,7 +59,7 @@ func processBody(resp *http.Response, config responseConfig, validator multiVali respBody, bodyLenBytes, bodyHash, respErr := readBody(resp, bufferBodyBytes) // If we encounter an error while reading the body just fail early if respErr != nil { - return nil, "", reason.IOFailed(respErr) + return nil, "", reason.IOFailed(fmt.Errorf("failed reading HTTP response body: %w", respErr)) } // Run any validations @@ -114,6 +115,12 @@ func readPrefixAndHash(body io.ReadCloser, maxPrefixSize int) (respSize int, pre n += m } + // The ErrUnexpectedEOF message can be confusing to users, so we clarify it here + if err == io.ErrUnexpectedEOF { + return 0, "", "", fmt.Errorf("connection closed unexpectedly: %w", err) + } + + // Note that io.EOF is distinct from io.ErrUnexpectedEOF if err != nil && err != io.EOF { return 0, "", "", err }