diff --git a/tools.go b/tools.go index ace5f40..4cb172c 100644 --- a/tools.go +++ b/tools.go @@ -1,7 +1,8 @@ +//go:build tools // +build tools package main import ( _ "golang.org/x/tools/cmd/stringer" -) \ No newline at end of file +) diff --git a/unifi/unifi.go b/unifi/unifi.go index a3d5adc..ef2a881 100644 --- a/unifi/unifi.go +++ b/unifi/unifi.go @@ -246,11 +246,22 @@ func (c *Client) do(ctx context.Context, method, relativeURL string, reqBody int if resp.StatusCode != 200 { errBody := struct { Meta meta `json:"meta"` + Data []struct { + Meta meta `json:"meta"` + } `json:"data"` }{} if err = json.NewDecoder(resp.Body).Decode(&errBody); err != nil { 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 {