Skip to content

Commit ca396ec

Browse files
committed
Adjust for 7.1 error handling
1 parent 6bb9559 commit ca396ec

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

tools.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
//go:build tools
12
// +build tools
23

34
package main
45

56
import (
67
_ "golang.org/x/tools/cmd/stringer"
7-
)
8+
)

unifi/unifi.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,22 @@ func (c *Client) do(ctx context.Context, method, relativeURL string, reqBody int
246246
if resp.StatusCode != 200 {
247247
errBody := struct {
248248
Meta meta `json:"meta"`
249+
Data []struct {
250+
Meta meta `json:"meta"`
251+
} `json:"data"`
249252
}{}
250253
if err = json.NewDecoder(resp.Body).Decode(&errBody); err != nil {
251254
return err
252255
}
253-
return fmt.Errorf("%w (%s) for %s %s", errBody.Meta.error(), resp.Status, method, url.String())
256+
var apiErr error
257+
if len(errBody.Data) > 0 && errBody.Data[0].Meta.RC == "error" {
258+
// check first error in data, should we look for more than one?
259+
apiErr = errBody.Data[0].Meta.error()
260+
}
261+
if apiErr == nil {
262+
apiErr = errBody.Meta.error()
263+
}
264+
return fmt.Errorf("%w (%s) for %s %s", apiErr, resp.Status, method, url.String())
254265
}
255266

256267
if respBody == nil || resp.ContentLength == 0 {

0 commit comments

Comments
 (0)