refine chart clint http response

Chart client eats the http error if not status ok, after refactor, the
real http response will be catched in core api.

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-04-23 15:08:31 +08:00
parent ac4dfd5973
commit 0d563fda9c
3 changed files with 49 additions and 37 deletions

View File

@ -3,6 +3,8 @@ package chartserver
import ( import (
"errors" "errors"
"fmt" "fmt"
commonhttp "github.com/goharbor/harbor/src/common/http"
hlog "github.com/goharbor/harbor/src/common/utils/log"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -45,7 +47,7 @@ func NewChartClient(credentail *Credential) *ChartClient { // Create http client
// GetContent get the bytes from the specified url // GetContent get the bytes from the specified url
func (cc *ChartClient) GetContent(addr string) ([]byte, error) { func (cc *ChartClient) GetContent(addr string) ([]byte, error) {
response, err := cc.sendRequest(addr, http.MethodGet, nil, []int{http.StatusOK}) response, err := cc.sendRequest(addr, http.MethodGet, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -56,17 +58,48 @@ func (cc *ChartClient) GetContent(addr string) ([]byte, error) {
} }
defer response.Body.Close() defer response.Body.Close()
if response.StatusCode != http.StatusOK {
text, err := extractError(content)
if err != nil {
return nil, err
}
return nil, &commonhttp.Error{
Code: response.StatusCode,
Message: text,
}
}
return content, nil return content, nil
} }
// DeleteContent sends deleting request to the addr to delete content // DeleteContent sends deleting request to the addr to delete content
func (cc *ChartClient) DeleteContent(addr string) error { func (cc *ChartClient) DeleteContent(addr string) error {
_, err := cc.sendRequest(addr, http.MethodDelete, nil, []int{http.StatusOK}) response, err := cc.sendRequest(addr, http.MethodDelete, nil)
return err if err != nil {
return err
}
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
text, err := extractError(content)
if err != nil {
return err
}
return &commonhttp.Error{
Code: response.StatusCode,
Message: text,
}
}
return nil
} }
// sendRequest sends requests to the addr with the specified spec // sendRequest sends requests to the addr with the specified spec
func (cc *ChartClient) sendRequest(addr string, method string, body io.Reader, expectedCodes []int) (*http.Response, error) { func (cc *ChartClient) sendRequest(addr string, method string, body io.Reader) (*http.Response, error) {
if len(strings.TrimSpace(addr)) == 0 { if len(strings.TrimSpace(addr)) == 0 {
return nil, errors.New("empty url is not allowed") return nil, errors.New("empty url is not allowed")
} }
@ -88,30 +121,9 @@ func (cc *ChartClient) sendRequest(addr string, method string, body io.Reader, e
response, err := cc.httpClient.Do(request) response, err := cc.httpClient.Do(request)
if err != nil { if err != nil {
hlog.Errorf("%s '%s' failed with error: %s", method, fullURI.Path, err)
return nil, err return nil, err
} }
isExpectedStatusCode := false
for _, eCode := range expectedCodes {
if eCode == response.StatusCode {
isExpectedStatusCode = true
break
}
}
if !isExpectedStatusCode {
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
defer response.Body.Close()
if err := extractError(content); err != nil {
return nil, err
}
return nil, fmt.Errorf("%s '%s' failed with error: %s", method, fullURI.Path, content)
}
return response, nil return response, nil
} }

View File

@ -16,22 +16,22 @@ const (
// Extract error object '{"error": "****---***"}' from the content if existing // Extract error object '{"error": "****---***"}' from the content if existing
// nil error will be returned if it does exist // nil error will be returned if it does exist
func extractError(content []byte) error { func extractError(content []byte) (text string, err error) {
if len(content) == 0 { if len(content) == 0 {
return nil return "", nil
} }
errorObj := make(map[string]string) errorObj := make(map[string]string)
err := json.Unmarshal(content, &errorObj) err = json.Unmarshal(content, &errorObj)
if err != nil { if err != nil {
return nil return "", err
} }
if errText, ok := errorObj["error"]; ok { if errText, ok := errorObj["error"]; ok {
return errors.New(errText) return errText, nil
} }
return nil return "", nil
} }
// Parse the redis configuration to the beego cache pattern // Parse the redis configuration to the beego cache pattern

View File

@ -186,7 +186,7 @@ func (cra *ChartRepositoryAPI) ListCharts() {
charts, err := chartController.ListCharts(cra.namespace) charts, err := chartController.ListCharts(cra.namespace)
if err != nil { if err != nil {
cra.SendInternalServerError(err) cra.ParseAndHandleError("fail to list charts", err)
return return
} }
@ -204,7 +204,7 @@ func (cra *ChartRepositoryAPI) ListChartVersions() {
versions, err := chartController.GetChart(cra.namespace, chartName) versions, err := chartController.GetChart(cra.namespace, chartName)
if err != nil { if err != nil {
cra.SendInternalServerError(err) cra.ParseAndHandleError("fail to get chart", err)
return return
} }
@ -234,7 +234,7 @@ func (cra *ChartRepositoryAPI) GetChartVersion() {
chartVersion, err := chartController.GetChartVersionDetails(cra.namespace, chartName, version) chartVersion, err := chartController.GetChartVersionDetails(cra.namespace, chartName, version)
if err != nil { if err != nil {
cra.SendInternalServerError(err) cra.ParseAndHandleError("fail to get chart version", err)
return return
} }
@ -267,7 +267,7 @@ func (cra *ChartRepositoryAPI) DeleteChartVersion() {
} }
if err := chartController.DeleteChartVersion(cra.namespace, chartName, version); err != nil { if err := chartController.DeleteChartVersion(cra.namespace, chartName, version); err != nil {
cra.SendInternalServerError(err) cra.ParseAndHandleError("fail to delete chart version", err)
return return
} }
} }
@ -360,7 +360,7 @@ func (cra *ChartRepositoryAPI) DeleteChart() {
// Remove labels from all the deleting chart versions under the chart // Remove labels from all the deleting chart versions under the chart
chartVersions, err := chartController.GetChart(cra.namespace, chartName) chartVersions, err := chartController.GetChart(cra.namespace, chartName)
if err != nil { if err != nil {
cra.SendInternalServerError(err) cra.ParseAndHandleError("fail to get chart", err)
return return
} }