Skip to content

Commit 630b746

Browse files
download: Surface bundle download errors via debug logging
This change logs the error response body at debug level. Since the errors could contain senstive info we don't include them in the status message. So this approach helps to get more information about the error at debug log level which is mostly used in a non-prod setup. Fixes: #6609 Signed-off-by: Ashutosh Narkar <[email protected]>
1 parent ea0dc02 commit 630b746

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

download/download.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download
405405
longPoll: d.longPollingEnabled,
406406
}, nil
407407
default:
408+
if d.logger.GetLevel() == logging.Debug && resp.Body != nil {
409+
body, err := io.ReadAll(resp.Body)
410+
if err == nil {
411+
d.logger.Debug("bundle download error response with response body: %s", body)
412+
}
413+
}
414+
408415
return nil, HTTPError{StatusCode: resp.StatusCode}
409416
}
410417
}

download/download_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,51 @@ func TestFailureUnexpected(t *testing.T) {
641641
}
642642
}
643643

644+
func TestFailureUnexpectedWithResponseBody(t *testing.T) {
645+
646+
ctx := context.Background()
647+
648+
expResp := "this is a bad http response"
649+
650+
fixture := newTestFixture(t)
651+
fixture.server.expCode = 500
652+
fixture.server.expResp = expResp
653+
654+
defer fixture.server.stop()
655+
656+
d := New(Config{}, fixture.client, "/bundles/test/bundle1")
657+
658+
logger := test.New()
659+
logger.SetLevel(logging.Debug)
660+
d.logger = logger
661+
662+
err := d.oneShot(ctx)
663+
if err == nil {
664+
t.Fatal("expected error")
665+
}
666+
var hErr HTTPError
667+
if !errors.As(err, &hErr) {
668+
t.Fatal("expected HTTPError")
669+
}
670+
if hErr.StatusCode != 500 {
671+
t.Fatal("expected status code 500")
672+
}
673+
674+
expectLogged := fmt.Sprintf("bundle download error response with response body: %s", expResp)
675+
676+
var found bool
677+
for _, entry := range logger.Entries() {
678+
if entry.Message == expectLogged {
679+
found = true
680+
break
681+
}
682+
}
683+
684+
if !found {
685+
t.Errorf("Expected log entry: %s", expectLogged)
686+
}
687+
}
688+
644689
func TestEtagInResponse(t *testing.T) {
645690
ctx := context.Background()
646691
fixture := newTestFixture(t)

download/testharness.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ type testServer struct {
257257
t *testing.T
258258
customAuth func(http.ResponseWriter, *http.Request) error
259259
expCode int
260+
expResp string
260261
expEtag string
261262
expAuth string
262263
bundles map[string]bundle.Bundle
@@ -374,6 +375,11 @@ func (t *testServer) handle(w http.ResponseWriter, r *http.Request) {
374375

375376
if t.expCode != 0 {
376377
w.WriteHeader(t.expCode)
378+
379+
if t.expResp != "" {
380+
w.Write([]byte(t.expResp))
381+
}
382+
377383
return
378384
}
379385

0 commit comments

Comments
 (0)