Skip to content

Commit

Permalink
add custom error for non-200 response from reward service (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
btamadio authored Feb 28, 2021
1 parent a95180e commit 986c7d8
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion http_reward_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,28 @@ func (h *HTTPSource) GetRewards(ctx context.Context, banditContext interface{})
}

if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf("Non-2XX response code from reward server: %s", http.StatusText(resp.StatusCode))
defer resp.Body.Close()
errMsg, _ := ioutil.ReadAll(resp.Body)
return nil, &ErrRewardNon2XX{
Url: h.url,
StatusCode: resp.StatusCode,
RespBody: string(errMsg),
}
}

return h.parser.Parse(resp.Body)
}

type ErrRewardNon2XX struct {
Url string
StatusCode int
RespBody string
}

func (e *ErrRewardNon2XX) Error() string {
return fmt.Sprintf("reward service \"%s\": [%d] %s", e.Url, e.StatusCode, e.RespBody)
}

// HTTPDoer is a basic interface for making HTTP requests. The net/http Client can be used or you can bring your own.
// Heimdall is a pretty good alternative client with some nice features: https://github.com/gojek/heimdall
type HttpDoer interface {
Expand Down

0 comments on commit 986c7d8

Please sign in to comment.