aboutsummaryrefslogtreecommitdiff
path: root/clients
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2022-11-27 22:01:01 -0800
committerMike Crute <mike@crute.us>2022-11-27 22:01:01 -0800
commit111c9fe48f216d023530952df44559304bc9f2e7 (patch)
treee1d1e18ed3310b2e548b27aa8336434dc7b51154 /clients
parenta52fc849ce071c77ce4dd922960918eccf39b137 (diff)
downloadgolib-111c9fe48f216d023530952df44559304bc9f2e7.tar.bz2
golib-111c9fe48f216d023530952df44559304bc9f2e7.tar.xz
golib-111c9fe48f216d023530952df44559304bc9f2e7.zip
clients/netbox: better API error handlingclients/netbox/v3.1.0
Diffstat (limited to 'clients')
-rw-r--r--clients/netbox/client.go8
-rw-r--r--clients/netbox/model.go13
2 files changed, 21 insertions, 0 deletions
diff --git a/clients/netbox/client.go b/clients/netbox/client.go
index 43ef64c..0cad733 100644
--- a/clients/netbox/client.go
+++ b/clients/netbox/client.go
@@ -36,6 +36,14 @@ func (c *BasicNetboxClient) makeRequestRaw(ctx context.Context, method, u string
36 } 36 }
37 defer res.Body.Close() 37 defer res.Body.Close()
38 38
39 if res.StatusCode != http.StatusOK {
40 apiError := &ApiError{Status: res.StatusCode}
41 if err = json.NewDecoder(res.Body).Decode(apiError); err != nil {
42 return fmt.Errorf("Netbox JSON decode error while parsing error with status %d: %w", res.StatusCode, err)
43 }
44 return apiError
45 }
46
39 if err = json.NewDecoder(res.Body).Decode(o); err != nil { 47 if err = json.NewDecoder(res.Body).Decode(o); err != nil {
40 return err 48 return err
41 } 49 }
diff --git a/clients/netbox/model.go b/clients/netbox/model.go
index 78f2ca6..40c4aa6 100644
--- a/clients/netbox/model.go
+++ b/clients/netbox/model.go
@@ -1,5 +1,18 @@
1package netbox 1package netbox
2 2
3import "fmt"
4
5type ApiError struct {
6 Status int
7 Detail string `json:"detail"`
8}
9
10func (e *ApiError) Error() string {
11 return fmt.Sprintf("Netbox API Error (Status %d): %s", e.Status, e.Detail)
12}
13
14var _ error = (*ApiError)(nil)
15
3type LabeledInt struct { 16type LabeledInt struct {
4 Value int `json:"value"` 17 Value int `json:"value"`
5 Label string `json:"label"` 18 Label string `json:"label"`