Skip to content

Commit

Permalink
fix discard real error
Browse files Browse the repository at this point in the history
  • Loading branch information
acim committed Dec 15, 2023
1 parent 9e5a97f commit a3425d2
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,17 @@ func (c *Client) do(ctx context.Context, method, path string, body io.Reader, qu
err = errors.Join(err, res.Body.Close())
}()

if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated || res.StatusCode != http.StatusNotFound {
var root root
var root root

if err := json.NewDecoder(res.Body).Decode(&root); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}

if root.Error.Code == http.StatusNotFound {
return nil, fmt.Errorf("%w: %s", ErrStatus, root.Error.Message)
}
if err := json.NewDecoder(res.Body).Decode(&root); err != nil {
return nil, errors.Join(err, fmt.Errorf("%w: %s", ErrStatus, res.Status))
}

return &root, nil
if root.Error.Code >= http.StatusBadRequest {
return nil, fmt.Errorf("%w: %s", ErrStatus, root.Error.Message)
}

return nil, fmt.Errorf("%w: %s", ErrStatus, res.Status)
return &root, nil
}

func WithClient(hc *http.Client) Option {
Expand Down

0 comments on commit a3425d2

Please sign in to comment.