Skip to content

Commit f379e2e

Browse files
authored
Merge pull request prometheus#16437 from zenador/unwrap-read-client-error
remote: Allow unwrapping of errors when reading from remote client
2 parents f4ca136 + b91c66c commit f379e2e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

storage/remote/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query, sortSeries bool)
384384
_ = httpResp.Body.Close()
385385

386386
cancel()
387-
return nil, fmt.Errorf("remote server %s returned http status %s: %s", c.urlString, httpResp.Status, string(body))
387+
err := errors.New(string(body))
388+
return nil, fmt.Errorf("remote server %s returned http status %s: %w", c.urlString, httpResp.Status, err)
388389
}
389390

390391
contentType := httpResp.Header.Get("Content-Type")

storage/remote/client_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func TestReadClient(t *testing.T) {
225225
expectedSamples [][]model.SamplePair
226226
expectedErrorContains string
227227
sortSeries bool
228+
unwrap bool
228229
}{
229230
{
230231
name: "sorted sampled response",
@@ -336,6 +337,14 @@ func TestReadClient(t *testing.T) {
336337
timeout: 5 * time.Millisecond,
337338
expectedErrorContains: "context deadline exceeded: request timed out after 5ms",
338339
},
340+
{
341+
name: "unwrap error",
342+
httpHandler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
343+
http.Error(w, "test error", http.StatusBadRequest)
344+
}),
345+
expectedErrorContains: "test error\n",
346+
unwrap: true,
347+
},
339348
}
340349

341350
for _, test := range tests {
@@ -366,6 +375,10 @@ func TestReadClient(t *testing.T) {
366375
ss, err := c.Read(context.Background(), query, test.sortSeries)
367376
if test.expectedErrorContains != "" {
368377
require.ErrorContains(t, err, test.expectedErrorContains)
378+
if test.unwrap {
379+
err = errors.Unwrap(err)
380+
require.EqualError(t, err, test.expectedErrorContains)
381+
}
369382
return
370383
}
371384

0 commit comments

Comments
 (0)