@@ -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.
145158Make 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}
0 commit comments