added checks for nil response argument, some prints for debugging current error

This commit is contained in:
Your Name 2019-02-09 18:24:52 +01:00
parent 46868b28df
commit 332fc651cc

View File

@ -6,6 +6,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"io/ioutil"
) )
// ------------------------------------------------------------- // -------------------------------------------------------------
@ -61,6 +63,9 @@ func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{}
} }
func (b *Bridge) putToBridge(endpoint string, payload interface{}, respData interface{}) error { func (b *Bridge) putToBridge(endpoint string, payload interface{}, respData interface{}) error {
// TODO: remove
fmt.Println("load:", payload)
data, errMarhshal := json.Marshal(payload) data, errMarhshal := json.Marshal(payload)
if errMarhshal != nil { if errMarhshal != nil {
return errMarhshal return errMarhshal
@ -78,14 +83,22 @@ func (b *Bridge) putToBridge(endpoint string, payload interface{}, respData inte
return err return err
} }
// TODO: remove
fmt.Println("response Status:", res.Status)
fmt.Println("response Headers:", res.Header)
body, _ := ioutil.ReadAll(res.Body)
fmt.Println("response Body:", string(body))
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
return errors.New("Hue responded with error" + res.Status + fmt.Sprint(res.StatusCode)) return errors.New("Hue responded with error" + res.Status + fmt.Sprint(res.StatusCode))
} }
// Unmarshal data // Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(respData) if respData != nil {
if errDecode != nil { errDecode := json.NewDecoder(res.Body).Decode(respData)
return errDecode if errDecode != nil {
return errDecode
}
} }
return nil return nil
@ -111,9 +124,11 @@ func (b *Bridge) getFromBridge(endpoint string, target interface{}) error {
} }
// Unmarshal data // Unmarshal data
errDecode := json.NewDecoder(res.Body).Decode(target) if target != nil {
if errDecode != nil { errDecode := json.NewDecoder(res.Body).Decode(target)
return errDecode if errDecode != nil {
return errDecode
}
} }
return nil return nil