Skip to content

Commit

Permalink
fix: 避免status_code>=400并且resp为空时err为nil (#7264)
Browse files Browse the repository at this point in the history
Co-authored-by: Qu Xuan <quxuan@yunionyun.com>
  • Loading branch information
ioito and Qu Xuan committed Jul 26, 2020
1 parent 59b85d1 commit a67f3e5
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions pkg/util/httputils/httputils.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit a67f3e5

Please sign in to comment.