Skip to content

Commit 3c7531f

Browse files
committed
Improve iotclient errors details
Some errors are returned without an err model. In such cases, the details of the response are in the error body.
1 parent 8034348 commit 3c7531f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

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)
35-
if !ok {
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 {
3642
return err
3743
}
44+
if detail, ok := body["detail"]; ok {
45+
return fmt.Errorf("%w: %s", err, detail)
46+
}
3847

39-
return fmt.Errorf("%w: %s", err, modErr.Detail)
48+
return err
4049
}

0 commit comments

Comments
 (0)