diff --git a/pkg/util/httputils/httputils.go b/pkg/util/httputils/httputils.go index 9290353587..efffb7a6f6 100644 --- a/pkg/util/httputils/httputils.go +++ b/pkg/util/httputils/httputils.go @@ -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{}