From 986c7d84226467b78cc8ee77ec2f4b0ed6ee7f7c Mon Sep 17 00:00:00 2001 From: Brian Amadio Date: Sun, 28 Feb 2021 12:28:00 -0800 Subject: [PATCH] add custom error for non-200 response from reward service (#11) --- http_reward_source.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/http_reward_source.go b/http_reward_source.go index 1c8eb6a..f761954 100644 --- a/http_reward_source.go +++ b/http_reward_source.go @@ -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 {