Skip to content

Commit

Permalink
Adjust for 7.1 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Jul 3, 2022
1 parent 6bb9559 commit 4d25173
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tools.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//go:build tools
// +build tools

package main

import (
_ "golang.org/x/tools/cmd/stringer"
)
)
13 changes: 12 additions & 1 deletion unifi/unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4d25173

Please sign in to comment.