Skip to content

Commit

Permalink
chore: fix how the errors are logged
Browse files Browse the repository at this point in the history
  • Loading branch information
taciturnaxolotl committed Dec 21, 2024
1 parent 0adf0ce commit f0bb225
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion routes/utils/heartbeat_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"

conf "github.com/hackclub/hackatime/config"
"github.com/hackclub/hackatime/models"
)

Expand All @@ -19,17 +20,27 @@ func ParseHeartbeats(r *http.Request) ([]*models.Heartbeat, error) {
r.Body.Close()
r.Body = io.NopCloser(bytes.NewBuffer(body))

conf.Log().Debug("Parsing heartbeat array")

// Try bulk first
var heartbeats []*models.Heartbeat
if err := json.Unmarshal(body, &heartbeats); err == nil {
return heartbeats, nil
} else {
err = fmt.Errorf("failed to parse heartbeat array: %v", err)
conf.Log().Error(err.Error())
}

conf.Log().Debug("Failed to parse heartbeat array, trying single")

// Try single if bulk fails
var heartbeat models.Heartbeat
if err := json.Unmarshal(body, &heartbeat); err == nil {
return []*models.Heartbeat{&heartbeat}, nil
} else {
conf.Log().Error(err.Error())
err = fmt.Errorf("failed to parse heartbeat: %v", err)
}

return nil, fmt.Errorf("failed to parse heartbeat data: %v", err)
return nil, err
}

0 comments on commit f0bb225

Please sign in to comment.