Package fitbit: Return errors instead of logging them.
[kraftakt.git] / fitbit / fitbit.go
index 33f2ccd..ef6282e 100644 (file)
@@ -6,6 +6,7 @@ import (
        "crypto/hmac"
        "crypto/sha1"
        "encoding/base64"
+       "encoding/hex"
        "encoding/json"
        "fmt"
        "io/ioutil"
@@ -70,6 +71,12 @@ func CheckSignature(ctx context.Context, payload []byte, rawSig string) bool {
        mac.Write(payload)
        signatureWant := mac.Sum(nil)
 
+       if !hmac.Equal(signatureGot, signatureWant) {
+               log.Debugf(ctx, "CheckSignature(): got %q, want %q",
+                       hex.EncodeToString(signatureGot),
+                       hex.EncodeToString(signatureWant))
+       }
+
        return hmac.Equal(signatureGot, signatureWant)
 }
 
@@ -221,8 +228,7 @@ func (c *Client) Subscribe(ctx context.Context, collection string) error {
 
        if res.StatusCode >= 400 && res.StatusCode != http.StatusConflict {
                data, _ := ioutil.ReadAll(res.Body)
-               log.Errorf(ctx, "creating %q subscription failed: status %d %q", collection, res.StatusCode, data)
-               return fmt.Errorf("creating %q subscription failed", collection)
+               return fmt.Errorf("creating %q subscription failed: status %d %q", collection, res.StatusCode, data)
        }
        if res.StatusCode == http.StatusConflict {
                log.Infof(ctx, "creating %q subscription: already exists", collection)
@@ -251,8 +257,7 @@ func (c *Client) unsubscribe(ctx context.Context, userID, collection, subscripti
 
        if res.StatusCode >= 400 && res.StatusCode != http.StatusNotFound {
                data, _ := ioutil.ReadAll(res.Body)
-               log.Errorf(ctx, "deleting %q subscription failed: status %d %q", collection, res.StatusCode, data)
-               return fmt.Errorf("deleting %q subscription failed", collection)
+               return fmt.Errorf("deleting %q subscription failed: status %d %q", collection, res.StatusCode, data)
        }
        if res.StatusCode == http.StatusNotFound {
                log.Infof(ctx, "deleting %q subscription: not found", collection)
@@ -370,8 +375,7 @@ func (c *Client) Profile(ctx context.Context) (*Profile, error) {
 
        if res.StatusCode >= 400 {
                data, _ := ioutil.ReadAll(res.Body)
-               log.Errorf(ctx, "reading profile failed: %s", data)
-               return nil, fmt.Errorf("HTTP %d error", res.StatusCode)
+               return nil, fmt.Errorf("reading profile failed: %s", data)
        }
 
        var data struct {