From fb11736e1a79f55ef95159c5758d78e7dcf20490 Mon Sep 17 00:00:00 2001 From: "loki-gh-app[bot]" <160051081+loki-gh-app[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:37:44 +0530 Subject: [PATCH] fix(aggregated_metrics): Fix the IsError method causing retries (backport k230) (#15298) Co-authored-by: Shantanu Alshi --- pkg/util/http.go | 2 +- pkg/util/http_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/pkg/util/http.go b/pkg/util/http.go index 2d9b8a7e29039..bd154c1a5f215 100644 --- a/pkg/util/http.go +++ b/pkg/util/http.go @@ -329,7 +329,7 @@ func ErrorTypeFromHTTPStatus(status int) string { } func IsError(status int) bool { - return status/200 != 0 + return status < 200 || status >= 300 } func IsServerError(status int) bool { diff --git a/pkg/util/http_test.go b/pkg/util/http_test.go index c41ba0eb1525d..fce7b0ab490ec 100644 --- a/pkg/util/http_test.go +++ b/pkg/util/http_test.go @@ -280,3 +280,59 @@ func TestErrorTypeFromHTTPStatus(t *testing.T) { }) } } + +func TestIsError(t *testing.T) { + tests := []struct { + name string + status int + expectedResult bool + }{ + { + name: "200 OK", + status: 200, + expectedResult: false, + }, + { + name: "201 Created", + status: 201, + expectedResult: false, + }, + { + name: "400 Bad Request", + status: 400, + expectedResult: true, + }, + { + name: "404 Not Found", + status: 404, + expectedResult: true, + }, + { + name: "429 Too Many Requests", + status: 429, + expectedResult: true, + }, + { + name: "500 Internal Server Error", + status: 500, + expectedResult: true, + }, + { + name: "503 Service Unavailable", + status: 503, + expectedResult: true, + }, + { + name: "600 Unknown", + status: 600, + expectedResult: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := util.IsError(tt.status) + assert.Equal(t, tt.expectedResult, result) + }) + } +}