Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc clean-up reported by golangci-lint #10842

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sdk/azcore/policy_body_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions sdk/azcore/policy_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions sdk/azcore/policy_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion sdk/azcore/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sdk/azcore/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
catalinaperalta marked this conversation as resolved.
Show resolved Hide resolved
r.Body.Close()
}
}
Expand Down Expand Up @@ -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
}
Expand Down