Skip to content

Commit 5c36bca

Browse files
committed
Merge pull request #4390 from mboersma/cli-non-json-errors
fix(client): show non-JSON HTTP errors
2 parents 3b2827a + f0dce41 commit 5c36bca

4 files changed

Lines changed: 59 additions & 16 deletions

File tree

client/cmd/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Register(controller string, username string, password string, email string,
2929
return err
3030
}
3131

32-
if err = client.CheckConection(httpClient, controllerURL); err != nil {
32+
if err = client.CheckConnection(httpClient, controllerURL); err != nil {
3333
return err
3434
}
3535

@@ -115,7 +115,7 @@ func Login(controller string, username string, password string, sslVerify bool)
115115
return err
116116
}
117117

118-
if err = client.CheckConection(httpClient, controllerURL); err != nil {
118+
if err = client.CheckConnection(httpClient, controllerURL); err != nil {
119119
return err
120120
}
121121

client/controller/client/http.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ func (c Client) Request(method string, path string, body []byte) (*http.Response
5757
return nil, err
5858
}
5959

60-
checkAPICompatability(res.Header.Get("DEIS_API_VERSION"))
60+
if err = checkForErrors(res, ""); err != nil {
61+
return nil, err
62+
}
63+
64+
checkAPICompatibility(res.Header.Get("DEIS_API_VERSION"))
6165

6266
return res, nil
6367
}
@@ -108,22 +112,32 @@ func checkForErrors(res *http.Response, body string) error {
108112
return nil
109113
}
110114

111-
bodyMap := make(map[string]interface{})
115+
// Read the response body if none was provided.
116+
if body == "" {
117+
defer res.Body.Close()
118+
resBody, err := ioutil.ReadAll(res.Body)
119+
if err != nil {
120+
return err
121+
}
122+
body = string(resBody)
123+
}
112124

125+
// Unmarshal the response as JSON, or return the status and body.
126+
bodyMap := make(map[string]interface{})
113127
if err := json.Unmarshal([]byte(body), &bodyMap); err != nil {
114-
return err
128+
return fmt.Errorf("\n%s\n%s\n", res.Status, body)
115129
}
116130

117-
errorMessage := "\n"
131+
errorMessage := fmt.Sprintf("\n%s\n", res.Status)
118132
for key, value := range bodyMap {
119133
switch v := value.(type) {
120134
case string:
121-
errorMessage += key + ": " + v + "\n"
135+
errorMessage += fmt.Sprintf("%s: %s\n", key, v)
122136
case []interface{}:
123137
for _, subValue := range v {
124138
switch sv := subValue.(type) {
125139
case string:
126-
errorMessage += key + ": " + sv + "\n"
140+
errorMessage += fmt.Sprintf("%s: %s\n", key, sv)
127141
default:
128142
fmt.Printf("Unexpected type in %s error message array. Contents: %v",
129143
reflect.TypeOf(value), sv)
@@ -135,12 +149,11 @@ func checkForErrors(res *http.Response, body string) error {
135149
}
136150
}
137151

138-
errorMessage += res.Status + "\n"
139152
return errors.New(errorMessage)
140153
}
141154

142-
// CheckConection checks that the user is connected to a network and the URL points to a valid controller.
143-
func CheckConection(client *http.Client, controllerURL url.URL) error {
155+
// CheckConnection checks that the user is connected to a network and the URL points to a valid controller.
156+
func CheckConnection(client *http.Client, controllerURL url.URL) error {
144157
errorMessage := `%s does not appear to be a valid Deis controller.
145158
Make sure that the Controller URI is correct and the server is running.`
146159

@@ -167,7 +180,7 @@ Make sure that the Controller URI is correct and the server is running.`
167180
return fmt.Errorf(errorMessage, baseURL)
168181
}
169182

170-
checkAPICompatability(res.Header.Get("DEIS_API_VERSION"))
183+
checkAPICompatibility(res.Header.Get("DEIS_API_VERSION"))
171184

172185
return nil
173186
}

client/controller/client/http_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestCheckConnection(t *testing.T) {
109109

110110
httpClient := CreateHTTPClient(false)
111111

112-
if err = CheckConection(httpClient, *u); err != nil {
112+
if err = CheckConnection(httpClient, *u); err != nil {
113113
t.Error(err)
114114
}
115115
}
@@ -147,16 +147,16 @@ func TestCheckErrors(t *testing.T) {
147147
t.Parallel()
148148

149149
expected := `
150+
404 NOT FOUND
150151
error: This is an error.
151152
error_array: This is an array.
152153
error_array: Foo!
153-
404 NOT FOUND
154154
`
155155
altExpected := `
156+
404 NOT FOUND
156157
error_array: This is an array.
157158
error_array: Foo!
158159
error: This is an error.
159-
404 NOT FOUND
160160
`
161161

162162
body := `
@@ -178,6 +178,36 @@ error: This is an error.
178178
if actual != expected && actual != altExpected {
179179
t.Errorf("Expected %s or %s, Got %s", expected, altExpected, actual)
180180
}
181+
182+
expected = `
183+
503 Service Temporarily Unavailable
184+
<html>
185+
<head><title>503 Service Temporarily Unavailable</title></head>
186+
<body bgcolor="white">
187+
<center><h1>503 Service Temporarily Unavailable</h1></center>
188+
<hr><center>nginx/1.9.4</center>
189+
</body>
190+
</html>
191+
`
192+
193+
body = `<html>
194+
<head><title>503 Service Temporarily Unavailable</title></head>
195+
<body bgcolor="white">
196+
<center><h1>503 Service Temporarily Unavailable</h1></center>
197+
<hr><center>nginx/1.9.4</center>
198+
</body>
199+
</html>`
200+
201+
res = http.Response{
202+
StatusCode: http.StatusServiceUnavailable,
203+
Status: "503 Service Temporarily Unavailable",
204+
}
205+
206+
actual = checkForErrors(&res, body).Error()
207+
208+
if actual != expected {
209+
t.Errorf("Expected %s, Got %s", expected, actual)
210+
}
181211
}
182212

183213
func TestCheckErrorsReturnsNil(t *testing.T) {

client/controller/client/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func locateSettingsFile() string {
1818
return path.Join(FindHome(), ".deis", filename+".json")
1919
}
2020

21-
func checkAPICompatability(serverAPIVersion string) {
21+
func checkAPICompatibility(serverAPIVersion string) {
2222
if serverAPIVersion != version.APIVersion {
2323
fmt.Printf(`! WARNING: Client and server API versions do not match. Please consider upgrading.
2424
! Client version: %s

0 commit comments

Comments
 (0)