Skip to content

Commit

Permalink
rework error messages including tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaedle committed Jun 27, 2024
1 parent fdbf1b2 commit 5e6df79
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 6 additions & 6 deletions samplers/aws/xray/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ func (c *xrayClient) getSamplingRules(ctx context.Context) (*getSamplingRulesOut

req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.samplingRulesURL, body)
if err != nil {
return nil, fmt.Errorf("xray client: failed to create http request: %w", err)
return nil, fmt.Errorf("unable to retrieve sampling rules, error on http request: %w", err)
}

output, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("xray client: unable to retrieve sampling settings: %w", err)
return nil, fmt.Errorf("xray client: unable to retrieve sampling rules, error on http request: %w", err)
}
defer func() { _ = output.Body.Close() }()

if output.StatusCode != http.StatusOK {
return nil, fmt.Errorf("xray client: unable to retrieve sampling settings, expected response status code 200, got: %d", output.StatusCode)
return nil, fmt.Errorf("xray client: unable to retrieve sampling rules, expected response status code 200, got: %d", output.StatusCode)
}

var samplingRulesOutput *getSamplingRulesOutput
if err := json.NewDecoder(output.Body).Decode(&samplingRulesOutput); err != nil {
return nil, fmt.Errorf("xray client: unable to unmarshal the response body: %w", err)
return nil, fmt.Errorf("xray client: unable to retrieve sampling rules, unable to unmarshal the response body: %w", err)
}

return samplingRulesOutput, nil
Expand All @@ -170,7 +170,7 @@ func (c *xrayClient) getSamplingTargets(ctx context.Context, s []*samplingStatis

output, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("xray client: unable to retrieve sampling settings: %w", err)
return nil, fmt.Errorf("xray client: unable to retrieve sampling targets, error on http request: %w", err)
}
defer func() { _ = output.Body.Close() }()

Expand All @@ -180,7 +180,7 @@ func (c *xrayClient) getSamplingTargets(ctx context.Context, s []*samplingStatis

var samplingTargetsOutput *getSamplingTargetsOutput
if err := json.NewDecoder(output.Body).Decode(&samplingTargetsOutput); err != nil {
return nil, fmt.Errorf("xray client: unable to unmarshal the response body: %w", err)
return nil, fmt.Errorf("xray client: unable to retrieve sampling targets, unable to unmarshal the response body: %w", err)
}

return samplingTargetsOutput, nil
Expand Down
8 changes: 5 additions & 3 deletions samplers/aws/xray/internal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,12 @@ func TestEndpointIsNotReachable(t *testing.T) {

actualRules, err := client.getSamplingRules(context.Background())
assert.Error(t, err)
assert.ErrorContains(t, err, "xray client: unable to retrieve sampling rules, error on http request: ")
assert.Nil(t, actualRules)

actualTargets, err := client.getSamplingTargets(context.Background(), nil)
assert.Error(t, err)
assert.ErrorContains(t, err, "xray client: unable to retrieve sampling targets, error on http request: ")
assert.Nil(t, actualTargets)
}

Expand All @@ -266,7 +268,7 @@ func TestRespondsWithErrorStatusCode(t *testing.T) {

actualRules, err := client.getSamplingRules(context.Background())
assert.Error(t, err)
assert.EqualError(t, err, fmt.Sprintf("xray client: unable to retrieve sampling settings, expected response status code 200, got: %d", http.StatusForbidden))
assert.EqualError(t, err, fmt.Sprintf("xray client: unable to retrieve sampling rules, expected response status code 200, got: %d", http.StatusForbidden))
assert.Nil(t, actualRules)

actualTargets, err := client.getSamplingTargets(context.Background(), nil)
Expand Down Expand Up @@ -297,12 +299,12 @@ func TestInvalidResponseBody(t *testing.T) {

assert.Error(t, err)
assert.Nil(t, actualRules)
assert.ErrorContains(t, err, "xray client: unable to unmarshal the response body:"+scenario.response)
assert.ErrorContains(t, err, "xray client: unable to retrieve sampling rules, unable to unmarshal the response body:"+scenario.response)

actualTargets, err := client.getSamplingTargets(context.TODO(), nil)
assert.Error(t, err)
assert.Nil(t, actualTargets)
assert.ErrorContains(t, err, "xray client: unable to unmarshal the response body: "+scenario.response)
assert.ErrorContains(t, err, "xray client: unable to retrieve sampling targets, unable to unmarshal the response body: "+scenario.response)
})
}
}

0 comments on commit 5e6df79

Please sign in to comment.