fix: 避免status_code>=400并且resp为空时err为nil (#7264)

Co-authored-by: Qu Xuan <quxuan@yunionyun.com>
This commit is contained in:
屈轩
2020-07-27 04:42:26 +08:00
committed by GitHub
parent 59b85d1634
commit a67f3e5481

View File

@@ -149,17 +149,17 @@ type JsonResponse interface {
}
func (ce *JSONClientError) ParseErrorFromJsonResponse(statusCode int, body jsonutils.JSONObject) error {
err := body.Unmarshal(ce)
if err != nil {
return errors.Wrapf(err, "body.Unmarshal(%s)", body.String())
body.Unmarshal(ce)
if ce.Code == 0 {
ce.Code = statusCode
}
if ce.Code != 0 || len(ce.Class) > 0 || len(ce.Class) > 0 || len(ce.Details) > 0 {
if ce.Code == 0 {
ce.Code = statusCode
}
return ce
if len(ce.Class) == 0 {
ce.Class = http.StatusText(statusCode)
}
return nil
if len(ce.Details) == 0 {
ce.Details = body.String()
}
return ce
}
func NewJsonClient(client *http.Client) *JsonClient {
@@ -544,11 +544,20 @@ func (client *JsonClient) Send(ctx context.Context, req JsonReuest, response Jso
ce.Code = resp.StatusCode
ce.Details = resp.Header.Get("Location")
ce.Class = "redirect"
return resp.Header, nil, &ce
return resp.Header, jrbody, &ce
}
return resp.Header, jrbody, response.ParseErrorFromJsonResponse(resp.StatusCode, jrbody)
}
func IsRedirectError(err error) bool {
ce, ok := err.(*JSONClientError)
if ok && ce.Class == "redirect" {
return true
}
return false
}
func ParseResponse(resp *http.Response, err error, debug bool) (http.Header, []byte, error) {
if err != nil {
ce := JSONClientError{}