Skip to content

Commit 1755334

Browse files
committed
Merge pull request #4068 from Joshua-Anderson/fix-defers
fix(client-go): defer deferred closing of body until after error check.
2 parents 2c6dfe4 + 82fafec commit 1755334

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

client-go/controller/client/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func Register(controllerURL url.URL, username string, password string, email str
3737
addUserAgent(&headers)
3838

3939
res, err := rawRequest(client, "POST", controllerURL.String(), bytes.NewBuffer(body), headers, 201)
40-
defer res.Body.Close()
4140

4241
if err != nil {
4342
return err
4443
}
44+
defer res.Body.Close()
4545

4646
fmt.Printf("Registered %s\n", username)
4747

@@ -72,11 +72,11 @@ func Login(controllerURL url.URL, username string, password string, sslVerify bo
7272
addUserAgent(&headers)
7373

7474
res, err := rawRequest(client, "POST", controllerURL.String(), bytes.NewBuffer(body), headers, 200)
75-
defer res.Body.Close()
7675

7776
if err != nil {
7877
return err
7978
}
79+
defer res.Body.Close()
8080

8181
resBody, err := ioutil.ReadAll(res.Body)
8282

client-go/controller/client/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ func (c Client) BasicRequest(method string, path string, body []byte) (string, i
8787
if err != nil {
8888
return "", -1, err
8989
}
90+
defer res.Body.Close()
9091

9192
resBody, err := ioutil.ReadAll(res.Body)
92-
defer res.Body.Close()
9393

9494
if err != nil {
9595
return "", -1, err
@@ -114,12 +114,12 @@ Make sure that the Controller URI is correct and the server is running.`
114114
}
115115

116116
res, err := client.Do(req)
117-
defer res.Body.Close()
118117

119118
if err != nil {
120119
fmt.Printf(errorMessage+"\n", baseURL)
121120
return err
122121
}
122+
defer res.Body.Close()
123123

124124
if res.StatusCode != 401 {
125125
return fmt.Errorf(errorMessage, baseURL)

0 commit comments

Comments
 (0)