From fdf5d95ac7ec5670bb41d43fdde29e93e0ceecb4 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Wed, 17 Nov 2021 12:40:33 +0100 Subject: [PATCH 1/2] 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. --- internal/iot/error.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/iot/error.go b/internal/iot/error.go index c54eba2d..6d2f679b 100644 --- a/internal/iot/error.go +++ b/internal/iot/error.go @@ -18,6 +18,7 @@ package iot import ( + "encoding/json" "fmt" iotclient "github.com/arduino/iot-client-go" @@ -32,9 +33,17 @@ func errorDetail(err error) error { } modErr, ok := apiErr.Model().(iotclient.ModelError) - if !ok { + if ok { + return fmt.Errorf("%w: %s", err, modErr.Detail) + } + + body := make(map[string]interface{}) + if bodyErr := json.Unmarshal(apiErr.Body(), &body); bodyErr != nil { return err } + if detail, ok := body["detail"]; ok { + return fmt.Errorf("%w: %s", err, detail) + } - return fmt.Errorf("%w: %s", err, modErr.Detail) + return err } From 08ba83afc3ae13c84646498d7859db4cad7c6e47 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Thu, 18 Nov 2021 12:16:28 +0100 Subject: [PATCH 2/2] Return whatever it's contained in detail --- internal/iot/error.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/iot/error.go b/internal/iot/error.go index 6d2f679b..00c205de 100644 --- a/internal/iot/error.go +++ b/internal/iot/error.go @@ -41,9 +41,9 @@ func errorDetail(err error) error { if bodyErr := json.Unmarshal(apiErr.Body(), &body); bodyErr != nil { return err } - if detail, ok := body["detail"]; ok { - return fmt.Errorf("%w: %s", err, detail) + detail, ok := body["detail"] + if !ok { + return err } - - return err + return fmt.Errorf("%w: %v", err, detail) }