Skip to content

Commit

Permalink
client: improve client error messages (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhowden committed Jun 28, 2022
1 parent 2894bd7 commit 44d6739
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (c *Client) Convert(r io.Reader, filename string) (*Response, error) {
if err != nil {
return nil, err
}
if _, err := io.Copy(part, r); err != nil {
return nil, err
if n, err := io.Copy(part, r); err != nil {
return nil, fmt.Errorf("could not copy file data into request (failed after %d bytes): %w", n, err)
}
if err := w.Close(); err != nil {
return nil, err
Expand All @@ -109,6 +109,16 @@ func (c *Client) Convert(r io.Reader, filename string) (*Response, error) {
defer resp.Body.Close()

res := &Response{}
if resp.StatusCode != http.StatusOK {
err := json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
// Invalid JSON can come from proxies etc, so try
// to give something meaningful.
return nil, fmt.Errorf("non-OK status from convert server: %d (%v)", resp.StatusCode, http.StatusText(resp.StatusCode))
}
return nil, fmt.Errorf("non-OK status from convert server: %d (%v) with error: %v", resp.StatusCode, http.StatusText(resp.StatusCode), res.Error)
}

if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
Expand Down

0 comments on commit 44d6739

Please sign in to comment.