Loki: Add error handling to CallResource (#64439)

* surface loki error message for `CallResource`

* use `data.Message` instead or `errorMessage`

* change struct coming from Loki

* remove whitespace
This commit is contained in:
Sven Grossmann
2023-03-09 11:12:33 +01:00
committed by GitHub
parent 1898e76dd6
commit 473013e3f5
3 changed files with 98 additions and 28 deletions
+42
View File
@@ -188,4 +188,46 @@ func TestApiReturnValues(t *testing.T) {
require.Equal(t, "gzip", encodedBytes.Encoding)
require.Equal(t, []byte("foo"), encodedBytes.Body)
})
t.Run("Loki should return the error as message", func(t *testing.T) {
called := false
api := makeCompressedMockedAPIWithUrl("http://localhost:3100", 400, "application/json", []byte("foo"), func(req *http.Request) {
called = true
})
encodedBytes, err := api.RawQuery(context.Background(), "/loki/api/v1/labels?start=1&end=2")
require.NoError(t, err)
require.True(t, called)
require.Equal(t, "gzip", encodedBytes.Encoding)
require.Equal(t, []byte("{\"message\":\"foo\"}"), encodedBytes.Body)
})
t.Run("Loki should return the error as is", func(t *testing.T) {
called := false
api := makeCompressedMockedAPIWithUrl("http://localhost:3100", 400, "application/json", []byte("{\"message\":\"foo\"}"), func(req *http.Request) {
called = true
})
encodedBytes, err := api.RawQuery(context.Background(), "/loki/api/v1/labels?start=1&end=2")
require.NoError(t, err)
require.True(t, called)
require.Equal(t, "gzip", encodedBytes.Encoding)
require.Equal(t, []byte("{\"message\":\"foo\"}"), encodedBytes.Body)
})
t.Run("Loki should not return the error on 500", func(t *testing.T) {
api := makeCompressedMockedAPIWithUrl("http://localhost:3100", 500, "application/json", []byte("foo"), nil)
_, err := api.RawQuery(context.Background(), "/loki/api/v1/labels?start=1&end=2")
require.Error(t, err)
require.ErrorContains(t, err, "foo")
})
t.Run("Loki should not return the error on 500 in JSON", func(t *testing.T) {
api := makeCompressedMockedAPIWithUrl("http://localhost:3100", 500, "application/json", []byte("{\"message\":\"foo\"}"), nil)
_, err := api.RawQuery(context.Background(), "/loki/api/v1/labels?start=1&end=2")
require.Error(t, err)
require.ErrorContains(t, err, "foo")
})
}