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:
@@ -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")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user