Skip to content

Commit

Permalink
Better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelreiswildlife committed Jul 26, 2023
1 parent 4893795 commit e5a8285
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions leaderboard/enriching/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,35 @@ func (e *enricherImpl) Enrich(tenantID, leaderboardID string, members []*model.M
body := membersModelToProto(leaderboardID, members)
jsonData, err := json.Marshal(podium_leaderboard_webhooks_v1.EnrichLeaderboardsRequest{Members: body})
if err != nil {
return nil, fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentInternal))
return nil, fmt.Errorf("could not marshal request: %w", errors.Join(err, ErrEnrichmentInternal))
}

webhookUrl, err := buildUrl(tenantUrl)
if err != nil {
return nil, fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentInternal))
return nil, fmt.Errorf("could not build webhook URL: %w", errors.Join(err, ErrEnrichmentInternal))
}

req, err := http.NewRequest(http.MethodPost, webhookUrl, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentInternal))
return nil, fmt.Errorf("could not create request: %w", errors.Join(err, ErrEnrichmentInternal))
}

req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentCall))
return nil, fmt.Errorf("could not complete request to webhook: %w", errors.Join(err, ErrEnrichmentCall))
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentCall))
return nil, fmt.Errorf("webhook returned non OK response: %w", errors.Join(err, ErrEnrichmentCall))
}

var result podium_leaderboard_webhooks_v1.EnrichLeaderboardsResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentCall))
return nil, fmt.Errorf("could not unmarshal webhook response: %w", errors.Join(err, ErrEnrichmentCall))
}

return protoToMemberModels(result.Members), nil
Expand All @@ -84,12 +84,12 @@ func buildUrl(baseUrl string) (string, error) {

u, err := url.JoinPath(baseUrl, enrichURL)
if err != nil {
return "", fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentInternal))
return "", fmt.Errorf("could not join url paths: %w", errors.Join(err, ErrEnrichmentInternal))
}

parsedUrl, err := url.ParseRequestURI(u)
if err != nil {
return "", fmt.Errorf("failed to call webhook: %w", errors.Join(err, ErrEnrichmentInternal))
return "", fmt.Errorf("could not parse url %s: %w", u, errors.Join(err, ErrEnrichmentInternal))
}

return parsedUrl.String(), nil
Expand Down

0 comments on commit e5a8285

Please sign in to comment.