Skip to content

Commit 0bf2d46

Browse files
authored
fix(errors): return api incompatibility errors
instead of bailing out immediately
1 parent 1fdf171 commit 0bf2d46

12 files changed

Lines changed: 85 additions & 80 deletions

File tree

apps/apps.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ func List(c *deis.Client, results int) ([]api.App, int, error) {
5252
func New(c *deis.Client, appID string) (api.App, error) {
5353
body := []byte{}
5454

55-
var err error
5655
if appID != "" {
5756
req := api.AppCreateRequest{ID: appID}
58-
body, err = json.Marshal(req)
57+
b, err := json.Marshal(req)
5958

6059
if err != nil {
6160
return api.App{}, err
6261
}
62+
body = b
6363
}
6464

65-
res, err := c.Request("POST", "/v2/apps/", body)
66-
if err != nil {
67-
return api.App{}, err
65+
res, reqErr := c.Request("POST", "/v2/apps/", body)
66+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
67+
return api.App{}, reqErr
6868
}
6969
// Fix json.Decoder bug in <go1.7
7070
defer func() {
@@ -73,23 +73,23 @@ func New(c *deis.Client, appID string) (api.App, error) {
7373
}()
7474

7575
app := api.App{}
76-
if err = json.NewDecoder(res.Body).Decode(&app); err != nil {
76+
if err := json.NewDecoder(res.Body).Decode(&app); err != nil {
7777
return api.App{}, err
7878
}
7979

8080
// Add in app URL based on controller hostname, port included
8181
app.URL = fmt.Sprintf("%s.%s", app.ID, strings.TrimPrefix(c.ControllerURL.Host, workflowURLPrefix))
8282

83-
return app, nil
83+
return app, reqErr
8484
}
8585

8686
// Get app details from a controller.
8787
func Get(c *deis.Client, appID string) (api.App, error) {
8888
u := fmt.Sprintf("/v2/apps/%s/", appID)
8989

90-
res, err := c.Request("GET", u, nil)
91-
if err != nil {
92-
return api.App{}, err
90+
res, reqErr := c.Request("GET", u, nil)
91+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
92+
return api.App{}, reqErr
9393
}
9494
// Fix json.Decoder bug in <go1.7
9595
defer func() {
@@ -99,14 +99,14 @@ func Get(c *deis.Client, appID string) (api.App, error) {
9999

100100
app := api.App{}
101101

102-
if err = json.NewDecoder(res.Body).Decode(&app); err != nil {
102+
if err := json.NewDecoder(res.Body).Decode(&app); err != nil {
103103
return api.App{}, err
104104
}
105105

106106
// Add in app URL based on controller hostname, port included
107107
app.URL = fmt.Sprintf("%s.%s", app.ID, strings.TrimPrefix(c.ControllerURL.Host, workflowURLPrefix))
108108

109-
return app, nil
109+
return app, reqErr
110110
}
111111

112112
// Logs retrieves logs from an app. The number of log lines fetched can be set by the lines
@@ -118,8 +118,8 @@ func Logs(c *deis.Client, appID string, lines int) (string, error) {
118118
u += "?log_lines=" + strconv.Itoa(lines)
119119
}
120120

121-
res, err := c.Request("GET", u, nil)
122-
if err != nil {
121+
res, reqErr := c.Request("GET", u, nil)
122+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
123123
return "", ErrNoLogs
124124
}
125125
defer res.Body.Close()
@@ -130,7 +130,7 @@ func Logs(c *deis.Client, appID string, lines int) (string, error) {
130130
}
131131

132132
// We need to trim a few characters off the front and end of the string
133-
return string(body[2 : len(body)-1]), nil
133+
return string(body[2 : len(body)-1]), reqErr
134134
}
135135

136136
// Run a one-time command in your app. This will start a kubernetes job with the
@@ -145,9 +145,9 @@ func Run(c *deis.Client, appID string, command string) (api.AppRunResponse, erro
145145

146146
u := fmt.Sprintf("/v2/apps/%s/run", appID)
147147

148-
res, err := c.Request("POST", u, body)
149-
if err != nil {
150-
return api.AppRunResponse{}, err
148+
res, reqErr := c.Request("POST", u, body)
149+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
150+
return api.AppRunResponse{}, reqErr
151151
}
152152

153153
arr := api.AppRunResponse{}
@@ -156,7 +156,7 @@ func Run(c *deis.Client, appID string, command string) (api.AppRunResponse, erro
156156
return api.AppRunResponse{}, err
157157
}
158158

159-
return arr, nil
159+
return arr, reqErr
160160
}
161161

162162
// Delete an app.

auth/auth.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func Login(c *deis.Client, username, password string) (string, error) {
3737
return "", err
3838
}
3939

40-
res, err := c.Request("POST", "/v2/auth/login/", reqBody)
41-
if err != nil {
42-
return "", err
40+
res, reqErr := c.Request("POST", "/v2/auth/login/", reqBody)
41+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
42+
return "", reqErr
4343
}
4444
// Fix json.Decoder bug in <go1.7
4545
defer func() {
@@ -52,7 +52,7 @@ func Login(c *deis.Client, username, password string) (string, error) {
5252
return "", err
5353
}
5454

55-
return token.Token, nil
55+
return token.Token, reqErr
5656
}
5757

5858
// Delete deletes a user.
@@ -101,9 +101,9 @@ func Regenerate(c *deis.Client, username string, all bool) (string, error) {
101101
return "", err
102102
}
103103

104-
res, err := c.Request("POST", "/v2/auth/tokens/", reqBody)
105-
if err != nil {
106-
return "", err
104+
res, reqErr := c.Request("POST", "/v2/auth/tokens/", reqBody)
105+
if err != nil && !deis.IsErrAPIMismatch(reqErr) {
106+
return "", reqErr
107107
}
108108
// Fix json.Decoder bug in <go1.7
109109
defer func() {
@@ -120,7 +120,7 @@ func Regenerate(c *deis.Client, username string, all bool) (string, error) {
120120
return "", err
121121
}
122122

123-
return token.Token, nil
123+
return token.Token, reqErr
124124
}
125125

126126
// Passwd changes a user's password.

builds/builds.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ func New(c *deis.Client, appID string, image string,
6868
return api.Build{}, err
6969
}
7070

71-
res, err := c.Request("POST", u, body)
72-
if err != nil {
73-
return api.Build{}, err
71+
res, reqErr := c.Request("POST", u, body)
72+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
73+
return api.Build{}, reqErr
7474
}
7575
// Fix json.Decoder bug in <go1.7
7676
defer func() {
@@ -83,5 +83,5 @@ func New(c *deis.Client, appID string, image string,
8383
return api.Build{}, err
8484
}
8585

86-
return build, nil
86+
return build, reqErr
8787
}

certs/certs.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func New(c *deis.Client, cert string, key string, name string) (api.Cert, error)
3434
return api.Cert{}, err
3535
}
3636

37-
res, err := c.Request("POST", "/v2/certs/", reqBody)
38-
if err != nil {
39-
return api.Cert{}, err
37+
res, reqErr := c.Request("POST", "/v2/certs/", reqBody)
38+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
39+
return api.Cert{}, reqErr
4040
}
4141
// Fix json.Decoder bug in <go1.7
4242
defer func() {
@@ -49,15 +49,15 @@ func New(c *deis.Client, cert string, key string, name string) (api.Cert, error)
4949
return api.Cert{}, err
5050
}
5151

52-
return resCert, nil
52+
return resCert, reqErr
5353
}
5454

5555
// Get information for a certificate
5656
func Get(c *deis.Client, name string) (api.Cert, error) {
5757
url := fmt.Sprintf("/v2/certs/%s", name)
58-
res, err := c.Request("GET", url, nil)
59-
if err != nil {
60-
return api.Cert{}, err
58+
res, reqErr := c.Request("GET", url, nil)
59+
if reqErr != nil {
60+
return api.Cert{}, reqErr
6161
}
6262
// Fix json.Decoder bug in <go1.7
6363
defer func() {
@@ -66,11 +66,11 @@ func Get(c *deis.Client, name string) (api.Cert, error) {
6666
}()
6767

6868
resCert := api.Cert{}
69-
if err = json.NewDecoder(res.Body).Decode(&resCert); err != nil {
69+
if err := json.NewDecoder(res.Body).Decode(&resCert); err != nil {
7070
return api.Cert{}, err
7171
}
7272

73-
return resCert, nil
73+
return resCert, reqErr
7474
}
7575

7676
// Delete removes a cert.

config/config.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
func List(c *deis.Client, app string) (api.Config, error) {
1616
u := fmt.Sprintf("/v2/apps/%s/config/", app)
1717

18-
res, err := c.Request("GET", u, nil)
19-
if err != nil {
20-
return api.Config{}, err
18+
res, reqErr := c.Request("GET", u, nil)
19+
if reqErr != nil {
20+
return api.Config{}, reqErr
2121
}
2222
// Fix json.Decoder bug in <go1.7
2323
defer func() {
@@ -26,11 +26,11 @@ func List(c *deis.Client, app string) (api.Config, error) {
2626
}()
2727

2828
config := api.Config{}
29-
if err = json.NewDecoder(res.Body).Decode(&config); err != nil {
29+
if err := json.NewDecoder(res.Body).Decode(&config); err != nil {
3030
return api.Config{}, err
3131
}
3232

33-
return config, nil
33+
return config, reqErr
3434
}
3535

3636
// Set sets an app's config variables and creates a new release.
@@ -53,9 +53,9 @@ func Set(c *deis.Client, app string, config api.Config) (api.Config, error) {
5353

5454
u := fmt.Sprintf("/v2/apps/%s/config/", app)
5555

56-
res, err := c.Request("POST", u, body)
57-
if err != nil {
58-
return api.Config{}, err
56+
res, reqErr := c.Request("POST", u, body)
57+
if reqErr != nil {
58+
return api.Config{}, reqErr
5959
}
6060
// Fix json.Decoder bug in <go1.7
6161
defer func() {
@@ -68,5 +68,5 @@ func Set(c *deis.Client, app string, config api.Config) (api.Config, error) {
6868
return api.Config{}, err
6969
}
7070

71-
return newConfig, nil
71+
return newConfig, reqErr
7272
}

deis.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ var (
9999
DefaultUserAgent = fmt.Sprintf("Deis Go SDK V%s", APIVersion)
100100
)
101101

102+
// IsErrAPIMismatch returns true if err is an ErrAPIMismatch, false otherwise
103+
func IsErrAPIMismatch(err error) bool {
104+
return err == ErrAPIMismatch
105+
}
106+
102107
// New creates a new client to communicate with the api.
103108
// The controllerURL is the url of the controller component, by default deis.<cluster url>.com
104109
// verifySSL determines whether or not to verify SSL connections.

domains/domains.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ func New(c *deis.Client, appID string, domain string) (api.Domain, error) {
4040
return api.Domain{}, err
4141
}
4242

43-
res, err := c.Request("POST", u, body)
44-
if err != nil {
45-
return api.Domain{}, err
43+
res, reqErr := c.Request("POST", u, body)
44+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
45+
return api.Domain{}, reqErr
4646
}
4747
// Fix json.Decoder bug in <go1.7
4848
defer func() {
@@ -55,7 +55,7 @@ func New(c *deis.Client, appID string, domain string) (api.Domain, error) {
5555
return api.Domain{}, err
5656
}
5757

58-
return d, nil
58+
return d, reqErr
5959
}
6060

6161
// Delete removes a domain from an app.

http.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ func (c *Client) Request(method string, path string, body []byte) (*http.Respons
7070

7171
// LimitedRequest allows limiting the number of responses in a request.
7272
func (c *Client) LimitedRequest(path string, results int) (string, int, error) {
73-
res, err := c.Request("GET", path+"?limit="+strconv.Itoa(results), nil)
73+
res, reqErr := c.Request("GET", path+"?limit="+strconv.Itoa(results), nil)
7474

75-
if err != nil {
76-
return "", -1, err
75+
if reqErr != nil && !IsErrAPIMismatch(reqErr) {
76+
return "", -1, reqErr
7777
}
7878

7979
defer res.Body.Close()
@@ -95,7 +95,7 @@ func (c *Client) LimitedRequest(path string, results int) (string, int, error) {
9595
return "", -1, err
9696
}
9797

98-
return string(out), int(r["count"].(float64)), nil
98+
return string(out), int(r["count"].(float64)), reqErr
9999
}
100100

101101
// CheckConnection checks that the user is connected to a network and the URL points to a valid controller.

keys/keys.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func New(c *deis.Client, id string, pubKey string) (api.Key, error) {
3434
req := api.KeyCreateRequest{ID: id, Public: pubKey}
3535
body, err := json.Marshal(req)
3636

37-
res, err := c.Request("POST", "/v2/keys/", body)
38-
if err != nil {
39-
return api.Key{}, err
37+
res, reqErr := c.Request("POST", "/v2/keys/", body)
38+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
39+
return api.Key{}, reqErr
4040
}
4141
// Fix json.Decoder bug in <go1.7
4242
defer func() {
@@ -49,7 +49,7 @@ func New(c *deis.Client, id string, pubKey string) (api.Key, error) {
4949
return api.Key{}, err
5050
}
5151

52-
return key, nil
52+
return key, reqErr
5353
}
5454

5555
// Delete removes a user's ssh key. The key ID will be the key comment, usually the email or user@hostname

perms/perms.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
// List users that can access an app.
1515
func List(c *deis.Client, appID string) ([]string, error) {
16-
res, err := c.Request("GET", fmt.Sprintf("/v2/apps/%s/perms/", appID), nil)
17-
if err != nil {
18-
return []string{}, err
16+
res, reqErr := c.Request("GET", fmt.Sprintf("/v2/apps/%s/perms/", appID), nil)
17+
if reqErr != nil && !deis.IsErrAPIMismatch(reqErr) {
18+
return []string{}, reqErr
1919
}
2020
// Fix json.Decoder bug in <go1.7
2121
defer func() {
@@ -24,11 +24,11 @@ func List(c *deis.Client, appID string) ([]string, error) {
2424
}()
2525

2626
var users api.PermsAppResponse
27-
if err = json.NewDecoder(res.Body).Decode(&users); err != nil {
27+
if err := json.NewDecoder(res.Body).Decode(&users); err != nil {
2828
return []string{}, err
2929
}
3030

31-
return users.Users, nil
31+
return users.Users, reqErr
3232
}
3333

3434
// ListAdmins lists deis platform administrators.

0 commit comments

Comments
 (0)