From 77549334701da78d3924abb378a71559ee4c7049 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Wed, 9 May 2018 16:11:52 +0200 Subject: [PATCH] fix(api): fix a panic issue when retrieving Docker API response --- api/http/proxy/response.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/api/http/proxy/response.go b/api/http/proxy/response.go index 4208f438c..751e3df31 100644 --- a/api/http/proxy/response.go +++ b/api/http/proxy/response.go @@ -13,6 +13,8 @@ import ( const ( // 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") + // 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{} { @@ -39,8 +41,17 @@ func getResponseAsJSONArray(response *http.Response) ([]interface{}, error) { return nil, err } - responseObject := responseData.([]interface{}) - return responseObject, nil + switch responseObject := responseData.(type) { + 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) {