Adjust for 7.1 error handling

This commit is contained in:
Paul Tyng
2022-07-02 20:11:12 -04:00
parent 6bb9559473
commit 4d251731a7
2 changed files with 14 additions and 2 deletions

View File

@@ -1,7 +1,8 @@
//go:build tools
// +build tools // +build tools
package main package main
import ( import (
_ "golang.org/x/tools/cmd/stringer" _ "golang.org/x/tools/cmd/stringer"
) )

View File

@@ -246,11 +246,22 @@ func (c *Client) do(ctx context.Context, method, relativeURL string, reqBody int
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
errBody := struct { errBody := struct {
Meta meta `json:"meta"` Meta meta `json:"meta"`
Data []struct {
Meta meta `json:"meta"`
} `json:"data"`
}{} }{}
if err = json.NewDecoder(resp.Body).Decode(&errBody); err != nil { if err = json.NewDecoder(resp.Body).Decode(&errBody); err != nil {
return err return err
} }
return fmt.Errorf("%w (%s) for %s %s", errBody.Meta.error(), resp.Status, method, url.String()) var apiErr error
if len(errBody.Data) > 0 && errBody.Data[0].Meta.RC == "error" {
// check first error in data, should we look for more than one?
apiErr = errBody.Data[0].Meta.error()
}
if apiErr == nil {
apiErr = errBody.Meta.error()
}
return fmt.Errorf("%w (%s) for %s %s", apiErr, resp.Status, method, url.String())
} }
if respBody == nil || resp.ContentLength == 0 { if respBody == nil || resp.ContentLength == 0 {