From 5e6df7907983042c484affeb2763c30eca1c98e5 Mon Sep 17 00:00:00 2001 From: Dennis Wielepsky Date: Thu, 27 Jun 2024 09:33:40 +0200 Subject: [PATCH] rework error messages including tests --- samplers/aws/xray/internal/client.go | 12 ++++++------ samplers/aws/xray/internal/client_test.go | 8 +++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/samplers/aws/xray/internal/client.go b/samplers/aws/xray/internal/client.go index f4badc08179..5fea4f358cc 100644 --- a/samplers/aws/xray/internal/client.go +++ b/samplers/aws/xray/internal/client.go @@ -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 @@ -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() }() @@ -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 diff --git a/samplers/aws/xray/internal/client_test.go b/samplers/aws/xray/internal/client_test.go index f2816d3256c..bcb433e0e69 100644 --- a/samplers/aws/xray/internal/client_test.go +++ b/samplers/aws/xray/internal/client_test.go @@ -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) } @@ -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) @@ -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) }) } }