From 50adcfe81630741dd5f6ea8fc1c32f1ab973f3b1 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Mon, 17 Apr 2023 10:38:19 -0700 Subject: [PATCH 1/3] Add err handling for imagerunner artifacts download --- internal/http/imagerunner.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/http/imagerunner.go b/internal/http/imagerunner.go index f9364d4de..34699f2cc 100644 --- a/internal/http/imagerunner.go +++ b/internal/http/imagerunner.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -13,6 +14,7 @@ import ( "github.com/hashicorp/go-retryablehttp" "github.com/saucelabs/saucectl/internal/iam" "github.com/saucelabs/saucectl/internal/imagerunner" + "github.com/saucelabs/saucectl/internal/msg" ) type ImageRunner struct { @@ -151,6 +153,16 @@ func (c *ImageRunner) DownloadArtifacts(ctx context.Context, id string) (io.Read if err != nil { return nil, err } + defer resp.Body.Close() + + if resp.StatusCode >= http.StatusInternalServerError { + return nil, errors.New(msg.InternalServerError) + } + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("request failed; unexpected response code: '%d', msg: '%v'", resp.StatusCode, string(body)) + } return resp.Body, nil } From 651100aac3fa5ec1e86a50c689ca4bc94b2b046e Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Mon, 17 Apr 2023 13:00:12 -0700 Subject: [PATCH 2/3] Update internal/http/imagerunner.go Co-authored-by: Alex Plischke --- internal/http/imagerunner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/http/imagerunner.go b/internal/http/imagerunner.go index 34699f2cc..055b78216 100644 --- a/internal/http/imagerunner.go +++ b/internal/http/imagerunner.go @@ -156,7 +156,7 @@ func (c *ImageRunner) DownloadArtifacts(ctx context.Context, id string) (io.Read defer resp.Body.Close() if resp.StatusCode >= http.StatusInternalServerError { - return nil, errors.New(msg.InternalServerError) + return nil, ErrServerError } if resp.StatusCode != http.StatusOK { From a57c31706be469a5b91fd9797f54e4b4b1a15432 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Mon, 17 Apr 2023 13:06:47 -0700 Subject: [PATCH 3/3] cleanup --- internal/http/imagerunner.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/http/imagerunner.go b/internal/http/imagerunner.go index 055b78216..04c825074 100644 --- a/internal/http/imagerunner.go +++ b/internal/http/imagerunner.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "io" "net/http" @@ -14,7 +13,6 @@ import ( "github.com/hashicorp/go-retryablehttp" "github.com/saucelabs/saucectl/internal/iam" "github.com/saucelabs/saucectl/internal/imagerunner" - "github.com/saucelabs/saucectl/internal/msg" ) type ImageRunner struct { @@ -153,7 +151,6 @@ func (c *ImageRunner) DownloadArtifacts(ctx context.Context, id string) (io.Read if err != nil { return nil, err } - defer resp.Body.Close() if resp.StatusCode >= http.StatusInternalServerError { return nil, ErrServerError