Skip to content

Commit e80be90

Browse files
committed
Improve iot client errors (#28)
Additional details are extracted from errors returned by iot-client-go. This change allows to return more appropriate error details.
1 parent 1111ba6 commit e80be90

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

Diff for: internal/iot/client.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package iot
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/antihax/optional"
@@ -50,7 +49,7 @@ func (cl *client) AddDevice(fqbn, name, serial, dType string) (string, error) {
5049
}
5150
dev, _, err := cl.api.DevicesV2Api.DevicesV2Create(cl.ctx, payload)
5251
if err != nil {
53-
err = fmt.Errorf("creating device, %w", err)
52+
err = fmt.Errorf("creating device, %w", errorDetail(err))
5453
return "", err
5554
}
5655
return dev.Id, nil
@@ -61,7 +60,7 @@ func (cl *client) AddDevice(fqbn, name, serial, dType string) (string, error) {
6160
func (cl *client) DeleteDevice(id string) error {
6261
_, err := cl.api.DevicesV2Api.DevicesV2Delete(cl.ctx, id)
6362
if err != nil {
64-
err = fmt.Errorf("deleting device: %w", err)
63+
err = fmt.Errorf("deleting device: %w", errorDetail(err))
6564
return err
6665
}
6766
return nil
@@ -72,7 +71,7 @@ func (cl *client) DeleteDevice(id string) error {
7271
func (cl *client) ListDevices() ([]iotclient.ArduinoDevicev2, error) {
7372
devices, _, err := cl.api.DevicesV2Api.DevicesV2List(cl.ctx, nil)
7473
if err != nil {
75-
err = fmt.Errorf("listing devices: %w", err)
74+
err = fmt.Errorf("listing devices: %w", errorDetail(err))
7675
return nil, err
7776
}
7877
return devices, nil
@@ -89,7 +88,7 @@ func (cl *client) AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2
8988

9089
newCert, _, err := cl.api.DevicesV2CertsApi.DevicesV2CertsCreate(cl.ctx, id, cert)
9190
if err != nil {
92-
err = fmt.Errorf("creating certificate, %w", err)
91+
err = fmt.Errorf("creating certificate, %w", errorDetail(err))
9392
return nil, err
9493
}
9594

@@ -99,12 +98,9 @@ func (cl *client) AddCertificate(id, csr string) (*iotclient.ArduinoCompressedv2
9998
// AddThing adds a new thing on Arduino IoT Cloud.
10099
func (cl *client) AddThing(thing *iotclient.Thing, force bool) (string, error) {
101100
opt := &iotclient.ThingsV2CreateOpts{Force: optional.NewBool(force)}
102-
newThing, resp, err := cl.api.ThingsV2Api.ThingsV2Create(cl.ctx, *thing, opt)
101+
newThing, _, err := cl.api.ThingsV2Api.ThingsV2Create(cl.ctx, *thing, opt)
103102
if err != nil {
104-
var respObj map[string]interface{}
105-
json.NewDecoder(resp.Body).Decode(&respObj)
106-
resp.Body.Close()
107-
return "", fmt.Errorf("%s: %s: %v", "adding new thing", err, respObj)
103+
return "", fmt.Errorf("%s: %w", "adding new thing", errorDetail(err))
108104
}
109105
return newThing.Id, nil
110106
}
@@ -114,7 +110,7 @@ func (cl *client) UpdateThing(id string, thing *iotclient.Thing, force bool) err
114110
opt := &iotclient.ThingsV2UpdateOpts{Force: optional.NewBool(force)}
115111
_, _, err := cl.api.ThingsV2Api.ThingsV2Update(cl.ctx, id, *thing, opt)
116112
if err != nil {
117-
return fmt.Errorf("%s: %v", "updating thing", err)
113+
return fmt.Errorf("%s: %v", "updating thing", errorDetail(err))
118114
}
119115
return nil
120116
}
@@ -123,7 +119,7 @@ func (cl *client) UpdateThing(id string, thing *iotclient.Thing, force bool) err
123119
func (cl *client) DeleteThing(id string) error {
124120
_, err := cl.api.ThingsV2Api.ThingsV2Delete(cl.ctx, id, nil)
125121
if err != nil {
126-
err = fmt.Errorf("deleting thing: %w", err)
122+
err = fmt.Errorf("deleting thing: %w", errorDetail(err))
127123
return err
128124
}
129125
return nil
@@ -134,7 +130,7 @@ func (cl *client) DeleteThing(id string) error {
134130
func (cl *client) GetThing(id string) (*iotclient.ArduinoThing, error) {
135131
thing, _, err := cl.api.ThingsV2Api.ThingsV2Show(cl.ctx, id, nil)
136132
if err != nil {
137-
err = fmt.Errorf("retrieving thing, %w", err)
133+
err = fmt.Errorf("retrieving thing, %w", errorDetail(err))
138134
return nil, err
139135
}
140136
return &thing, nil
@@ -155,7 +151,7 @@ func (cl *client) ListThings(ids []string, device *string, props bool) ([]iotcli
155151

156152
things, _, err := cl.api.ThingsV2Api.ThingsV2List(cl.ctx, opts)
157153
if err != nil {
158-
err = fmt.Errorf("retrieving things, %w", err)
154+
err = fmt.Errorf("retrieving things, %w", errorDetail(err))
159155
return nil, err
160156
}
161157
return things, nil

Diff for: internal/iot/error.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package iot
2+
3+
import (
4+
"fmt"
5+
6+
iotclient "github.com/arduino/iot-client-go"
7+
)
8+
9+
// errorDetail takes a generic iot-client-go error
10+
// and tries to return a more detailed error.
11+
func errorDetail(err error) error {
12+
apiErr, ok := err.(iotclient.GenericOpenAPIError)
13+
if !ok {
14+
return err
15+
}
16+
17+
modErr, ok := apiErr.Model().(iotclient.ModelError)
18+
if !ok {
19+
return err
20+
}
21+
22+
return fmt.Errorf("%w: %s", err, modErr.Detail)
23+
}

0 commit comments

Comments
 (0)