Return a 400 instead of a 500 for empty query (#98841)

This commit is contained in:
Sarah Zinger
2025-01-13 14:58:13 -05:00
committed by GitHub
parent ab6d82a33c
commit 210d73188d
2 changed files with 13 additions and 5 deletions

View File

@ -124,7 +124,14 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
logger.Warn("Found query models without targets", "models without targets", strings.Join(emptyQueries, "\n"))
// If no queries had a valid target, return an error; otherwise, attempt with the targets we have
if len(emptyQueries) == len(req.Queries) {
return &result, errors.New("no query target found for the alert rule")
if result.Responses == nil {
result.Responses = make(map[string]backend.DataResponse)
}
// marking this downstream error as it is a user error, but arguably this is a plugin error
// since the plugin should have frontend validation that prevents us from getting into this state
missingQueryResponse := backend.ErrDataResponseWithSource(400, backend.ErrorSourceDownstream, "no query target found for the alert rule")
result.Responses["A"] = missingQueryResponse
return &result, nil
}
}
formData["target"] = targetList

View File

@ -125,7 +125,7 @@ func TestProcessQueries(t *testing.T) {
assert.Equal(t, expectedInvalid, invalids[0])
})
t.Run("QueryData with no valid queries returns an error", func(t *testing.T) {
t.Run("QueryData with no valid queries returns bad request response", func(t *testing.T) {
queries := []backend.DataQuery{
{
RefID: "A",
@ -142,11 +142,12 @@ func TestProcessQueries(t *testing.T) {
}
service.im = fakeInstanceManager{}
_, err := service.QueryData(context.Background(), &backend.QueryDataRequest{
rsp, err := service.QueryData(context.Background(), &backend.QueryDataRequest{
Queries: queries,
})
assert.Error(t, err)
assert.Equal(t, err.Error(), "no query target found for the alert rule")
assert.NoError(t, err)
expectedResponse := backend.ErrDataResponseWithSource(400, backend.ErrorSourceDownstream, "no query target found for the alert rule")
assert.Equal(t, expectedResponse, rsp.Responses["A"])
})
}