fix(api): fix a panic issue when retrieving Docker API response

pull/1884/head
Anthony Lapenna 2018-05-09 16:11:52 +02:00
parent 3b14e6b6b9
commit 7754933470
1 changed files with 13 additions and 2 deletions

View File

@ -13,6 +13,8 @@ import (
const ( const (
// ErrEmptyResponseBody defines an error raised when portainer excepts to parse the body of a HTTP response and there is nothing to parse // ErrEmptyResponseBody defines an error raised when portainer excepts to parse the body of a HTTP response and there is nothing to parse
ErrEmptyResponseBody = portainer.Error("Empty response body") ErrEmptyResponseBody = portainer.Error("Empty response body")
// ErrInvalidResponseContent defines an error raised when Portainer excepts a JSON array and get something else.
ErrInvalidResponseContent = portainer.Error("Invalid Docker response")
) )
func extractJSONField(jsonObject map[string]interface{}, key string) map[string]interface{} { func extractJSONField(jsonObject map[string]interface{}, key string) map[string]interface{} {
@ -39,8 +41,17 @@ func getResponseAsJSONArray(response *http.Response) ([]interface{}, error) {
return nil, err return nil, err
} }
responseObject := responseData.([]interface{}) switch responseObject := responseData.(type) {
return responseObject, nil case []interface{}:
return responseObject, nil
case map[string]interface{}:
if responseObject["message"] != nil {
return nil, portainer.Error(responseObject["message"].(string))
}
return nil, ErrInvalidResponseContent
default:
return nil, ErrInvalidResponseContent
}
} }
func getResponseBodyAsGenericJSON(response *http.Response) (interface{}, error) { func getResponseBodyAsGenericJSON(response *http.Response) (interface{}, error) {