ElasticSearch: Improve ES error handling message (#61471)

Look for 'caused_by.reason' in ES error response

When the ES response does not contain `reason` or `root_cause[0].reason`
is empty, there is no information for the user to know what is going
wrong.

An example of the error message after this change:
```
Failed to evaluate queries and expressions: failed to execute query A: Trying to create too many buckets. Must be less than or equal to: [65536] but this number of buckets was exceeded. This limit can be set by changing the [search.max_buckets] cluster level setting.
```

Related to https://github.com/grafana/grafana/issues/61246
This commit is contained in:
Ruud van der Weijde 2023-01-20 11:46:31 +01:00 committed by GitHub
parent 3e86a1b3d9
commit b44df6c8d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -711,12 +711,15 @@ func getErrorFromElasticResponse(response *es.SearchResponse) string {
json := simplejson.NewFromAny(response.Error)
reason := json.Get("reason").MustString()
rootCauseReason := json.Get("root_cause").GetIndex(0).Get("reason").MustString()
causedByReason := json.Get("caused_by").Get("reason").MustString()
switch {
case rootCauseReason != "":
errorString = rootCauseReason
case reason != "":
errorString = reason
case causedByReason != "":
errorString = causedByReason
default:
errorString = "Unknown elasticsearch error response"
}