From 1755fe69681f9960e445bd54493e266c644c0662 Mon Sep 17 00:00:00 2001 From: Christoph Ostarek Date: Mon, 23 Jan 2023 14:59:28 +0100 Subject: [PATCH] zedcloud: print http response content on error print the http body for responses from zedcloud; this is helpful for debugging and instructing the user on how to fix an encountered error. Signed-off-by: Christoph Ostarek --- pkg/pillar/zedcloud/send.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/pillar/zedcloud/send.go b/pkg/pillar/zedcloud/send.go index a9009541558..dfa61916add 100644 --- a/pkg/pillar/zedcloud/send.go +++ b/pkg/pillar/zedcloud/send.go @@ -10,6 +10,7 @@ import ( "context" "crypto/tls" "crypto/x509" + "encoding/hex" "errors" "fmt" "io" @@ -939,9 +940,18 @@ func SendOnIntf(workContext context.Context, ctx *ZedCloudContext, destURL strin rv.RespContents = contents return rv, nil default: - errStr := fmt.Sprintf("SendOnIntf to %s reqlen %d statuscode %d %s", + maxlen := 256 + if maxlen > len(contents) { + maxlen = len(contents) + } + + hexdump := hex.Dump(bytes.TrimSpace(contents[:maxlen])) + // remove trailing newline from hex.Dump + hexdump = strings.TrimSuffix(hexdump, "\n") + + errStr := fmt.Sprintf("SendOnIntf to %s reqlen %d statuscode %d %s body:\n%s", reqUrl, reqlen, resp.StatusCode, - http.StatusText(resp.StatusCode)) + http.StatusText(resp.StatusCode), hexdump) // zedrouter probing sends 'http' to zedcloud server, expect to get status of 404, not an error if resp.StatusCode != http.StatusNotFound || ctx.AgentName != "zedrouter" { log.Errorln(errStr)