From a67f3e5481c2f00295e2dcd9ce256d96efa3fdfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Mon, 27 Jul 2020 04:42:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=81=BF=E5=85=8Dstatus=5Fcode>=3D400?= =?UTF-8?q?=E5=B9=B6=E4=B8=94resp=E4=B8=BA=E7=A9=BA=E6=97=B6err=E4=B8=BAni?= =?UTF-8?q?l=20(#7264)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Qu Xuan --- pkg/util/httputils/httputils.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) 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{}