From 6f4567e0237c65e8ea9bb19891e7e8b7c9ebf193 Mon Sep 17 00:00:00 2001 From: Asmir Avdicevic Date: Thu, 4 Mar 2021 13:49:24 +0100 Subject: [PATCH] fix(http): fix httpClient error checking --- lib/datasets_test.go | 3 +-- lib/dispatch.go | 2 +- lib/http.go | 44 +++++++++++++++++++++++++++++++++----------- lib/params.go | 10 ++++++++++ 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/datasets_test.go b/lib/datasets_test.go index ce42854c3..f69b1f811 100644 --- a/lib/datasets_test.go +++ b/lib/datasets_test.go @@ -1155,8 +1155,7 @@ func TestDatasetRequestsValidateFSI(t *testing.T) { m := NewDatasetMethods(tr.Instance) vp := &ValidateParams{Ref: refstr} - _, err := m.Validate(ctx, vp) - if err != nil { + if _, err := m.Validate(ctx, vp); err != nil { t.Fatal(err) } } diff --git a/lib/dispatch.go b/lib/dispatch.go index 46c580321..32eb2bab8 100644 --- a/lib/dispatch.go +++ b/lib/dispatch.go @@ -58,7 +58,7 @@ func (inst *Instance) Dispatch(ctx context.Context, method string, param interfa // another cannot modify the out-of-scope data of the other. This will mostly // involve making copies of the right things scope := scope{ - ctx: ctx, + ctx: ctx, inst: inst, } diff --git a/lib/http.go b/lib/http.go index 0c3cea0eb..40290581e 100644 --- a/lib/http.go +++ b/lib/http.go @@ -122,10 +122,11 @@ func (c HTTPClient) do(ctx context.Context, addr string, httpMethod string, mime return err } + if err = c.checkError(res, body, raw); err != nil { + return err + } + if raw { - if res.StatusCode < 200 && res.StatusCode > 299 { - return fmt.Errorf("HTTPClient req error: %d - %q", res.StatusCode, body) - } if buf, ok := result.(*bytes.Buffer); ok { buf.Write(body) } else { @@ -140,18 +141,39 @@ func (c HTTPClient) do(ctx context.Context, addr string, httpMethod string, mime } err = json.Unmarshal(body, &resData) if err != nil { - log.Debugf("HTTPClient unmarshal err: %s", err.Error()) - return fmt.Errorf("HTTPClient unmarshal err: %s", err) + log.Debugf("HTTPClient response err: %s", err.Error()) + return fmt.Errorf("HTTPClient response err: %s", err) } - return c.checkError(resData.Meta) + return nil } -func (c HTTPClient) checkError(meta *apiutil.Meta) error { - if meta == nil { - return fmt.Errorf("HTTPClient req error: invalid meta response") +func (c HTTPClient) checkError(res *http.Response, body []byte, raw bool) error { + metaResponse := struct { + Meta *apiutil.Meta + }{ + Meta: &apiutil.Meta{}, + } + parseErr := json.Unmarshal(body, &metaResponse) + if parseErr != nil { + if !raw { + log.Debugf("HTTPClient response error: %d - %q", res.StatusCode, body) + return fmt.Errorf("failed parsing response: %q", string(body)) + } + } + + if metaResponse.Meta == nil { + if !raw { + log.Debugf("HTTPClient response error: %d - %q", res.StatusCode, body) + return fmt.Errorf("invalid meta response") + } + } else if metaResponse.Meta.Code < 200 || metaResponse.Meta.Code > 299 { + log.Debugf("HTTPClient response meta error: %d - %q", metaResponse.Meta.Code, metaResponse.Meta.Error) + return fmt.Errorf(metaResponse.Meta.Error) } - if meta.Code != 200 { - return fmt.Errorf("HTTPClient req error: %d - %q", meta.Code, meta.Error) + + if res.StatusCode < 200 || res.StatusCode > 299 { + log.Debugf("HTTPClient response error: %d - %q", res.StatusCode, body) + return fmt.Errorf(string(body)) } return nil } diff --git a/lib/params.go b/lib/params.go index cfef088ea..2aac659cf 100644 --- a/lib/params.go +++ b/lib/params.go @@ -66,6 +66,16 @@ func (p *ListParams) UnmarshalFromRequest(r *http.Request) error { } else { lp.Raw = p.Raw } + if p.Peername == "" { + lp.Peername = r.FormValue("peername") + } else { + lp.Peername = p.Peername + } + if p.Term == "" { + lp.Term = r.FormValue("term") + } else { + lp.Term = p.Term + } *p = lp return nil }