From 28597315e4eba9eb2e1b66797200a3e8b18e709a Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Fri, 26 Jun 2020 16:05:34 -0700 Subject: [PATCH] Misc clean-up reported by golangci-lint Only remaining issues is code in error.go. --- sdk/azcore/policy_body_download.go | 3 ++- sdk/azcore/policy_logging.go | 6 +++--- sdk/azcore/policy_retry_test.go | 28 +++++++++++++++++++++------- sdk/azcore/progress_test.go | 4 +++- sdk/azcore/response.go | 4 ++-- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/sdk/azcore/policy_body_download.go b/sdk/azcore/policy_body_download.go index 22f5a1b9b415..63d2412f014e 100644 --- a/sdk/azcore/policy_body_download.go +++ b/sdk/azcore/policy_body_download.go @@ -23,7 +23,8 @@ func newBodyDownloadPolicy() Policy { if req.OperationValue(&opValues); !opValues.skip && resp.Body != nil { // Either bodyDownloadPolicyOpValues was not specified (so skip is false) // or it was specified and skip is false: don't skip downloading the body - b, err := ioutil.ReadAll(resp.Body) + var b []byte + b, err = ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { err = fmt.Errorf("body download policy: %w", err) diff --git a/sdk/azcore/policy_logging.go b/sdk/azcore/policy_logging.go index 5900e64f9c04..9f7691e55992 100644 --- a/sdk/azcore/policy_logging.go +++ b/sdk/azcore/policy_logging.go @@ -82,10 +82,10 @@ func (p *requestLogPolicy) Do(ctx context.Context, req *Request) (*Response, err if err == nil { // We got a response from the service sc := response.StatusCode + // Promote to Error any 4xx (except those listed is an error) or any 5xx. + // For other status codes, we leave the level as is. if ((sc >= 400 && sc <= 499) && sc != http.StatusNotFound && sc != http.StatusConflict && sc != http.StatusPreconditionFailed && sc != http.StatusRequestedRangeNotSatisfiable) || (sc >= 500 && sc <= 599) { - logClass = LogError // Promote to Error any 4xx (except those listed is an error) or any 5xx - } else { - // For other status codes, we leave the level as is. + logClass = LogError } } else { // This error did not get an HTTP response from the service; upgrade the severity to Error logClass = LogError diff --git a/sdk/azcore/policy_retry_test.go b/sdk/azcore/policy_retry_test.go index 139dfe4598b3..d3c51470c3a8 100644 --- a/sdk/azcore/policy_retry_test.go +++ b/sdk/azcore/policy_retry_test.go @@ -30,7 +30,9 @@ func TestRetryPolicySuccess(t *testing.T) { pl := NewPipeline(srv, NewRetryPolicy(nil)) req := NewRequest(http.MethodGet, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } resp, err := pl.Do(context.Background(), req) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -53,7 +55,9 @@ func TestRetryPolicyFailOnStatusCode(t *testing.T) { pl := NewPipeline(srv, NewRetryPolicy(testRetryOptions())) req := NewRequest(http.MethodGet, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } resp, err := pl.Do(context.Background(), req) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -81,7 +85,9 @@ func TestRetryPolicySuccessWithRetry(t *testing.T) { pl := NewPipeline(srv, NewRetryPolicy(testRetryOptions())) req := NewRequest(http.MethodGet, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } resp, err := pl.Do(context.Background(), req) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -108,7 +114,9 @@ func TestRetryPolicyFailOnError(t *testing.T) { pl := NewPipeline(srv, NewRetryPolicy(testRetryOptions())) req := NewRequest(http.MethodPost, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } resp, err := pl.Do(context.Background(), req) if !errors.Is(err, fakeErr) { t.Fatalf("unexpected error: %v", err) @@ -137,7 +145,9 @@ func TestRetryPolicySuccessWithRetryComplex(t *testing.T) { pl := NewPipeline(srv, NewRetryPolicy(testRetryOptions())) req := NewRequest(http.MethodGet, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } resp, err := pl.Do(context.Background(), req) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -163,7 +173,9 @@ func TestRetryPolicyRequestTimedOut(t *testing.T) { pl := NewPipeline(srv, NewRetryPolicy(nil)) req := NewRequest(http.MethodPost, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() resp, err := pl.Do(ctx, req) @@ -225,7 +237,9 @@ func TestWithRetryOptions(t *testing.T) { retryCtx := WithRetryOptions(context.Background(), customOptions) req := NewRequest(http.MethodGet, srv.URL()) body := newRewindTrackingBody("stuff") - req.SetBody(body) + if err := req.SetBody(body); err != nil { + t.Fatal(err) + } resp, err := pl.Do(retryCtx, req) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/sdk/azcore/progress_test.go b/sdk/azcore/progress_test.go index 61c3830175f0..9e385eeb3092 100644 --- a/sdk/azcore/progress_test.go +++ b/sdk/azcore/progress_test.go @@ -33,7 +33,9 @@ func TestProgressReporting(t *testing.T) { reqRpt := NewRequestBodyProgress(NopCloser(body), func(bytesTransferred int64) { bytesSent = bytesTransferred }) - req.SetBody(reqRpt) + if err := req.SetBody(reqRpt); err != nil { + t.Fatal(err) + } resp, err := pl.Do(context.Background(), req) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/sdk/azcore/response.go b/sdk/azcore/response.go index 3f018acd82ce..1665a24994be 100644 --- a/sdk/azcore/response.go +++ b/sdk/azcore/response.go @@ -114,7 +114,7 @@ func (r *Response) UnmarshalAsXML(v interface{}) error { // Drain reads the response body to completion then closes it. The bytes read are discarded. func (r *Response) Drain() { if r != nil && r.Body != nil { - io.Copy(ioutil.Discard, r.Body) + _, _ = io.Copy(ioutil.Discard, r.Body) r.Body.Close() } } @@ -150,7 +150,7 @@ func RetryAfter(resp *http.Response) time.Duration { if retryAfter, _ := strconv.Atoi(ra); retryAfter > 0 { return time.Duration(retryAfter) * time.Second } else if t, err := time.Parse(time.RFC1123, ra); err == nil { - return t.Sub(time.Now()) + return time.Until(t) } return 0 }