Skip to content

Commit 975a828

Browse files
author
Paolo Calao
authored
Improve iotclient error details (#62)
Some errors are returned without an err model. In such cases, the details of the response are in the error body.
1 parent b352a44 commit 975a828

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Diff for: internal/iot/error.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package iot
1919

2020
import (
21+
"encoding/json"
2122
"fmt"
2223

2324
iotclient "github.com/arduino/iot-client-go"
@@ -32,9 +33,17 @@ func errorDetail(err error) error {
3233
}
3334

3435
modErr, ok := apiErr.Model().(iotclient.ModelError)
36+
if ok {
37+
return fmt.Errorf("%w: %s", err, modErr.Detail)
38+
}
39+
40+
body := make(map[string]interface{})
41+
if bodyErr := json.Unmarshal(apiErr.Body(), &body); bodyErr != nil {
42+
return err
43+
}
44+
detail, ok := body["detail"]
3545
if !ok {
3646
return err
3747
}
38-
39-
return fmt.Errorf("%w: %s", err, modErr.Detail)
48+
return fmt.Errorf("%w: %v", err, detail)
4049
}

0 commit comments

Comments
 (0)