Skip to content

Commit

Permalink
fix(http): fix httpClient error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Arqu committed Mar 4, 2021
1 parent cc79182 commit 6f4567e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
3 changes: 1 addition & 2 deletions lib/datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
44 changes: 33 additions & 11 deletions lib/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
10 changes: 10 additions & 0 deletions lib/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6f4567e

Please sign in to comment.