Skip to content

Commit 3b56a94

Browse files
committed
Issue #28: handle errors coming from quickwit
1 parent a7603bf commit 3b56a94

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

pkg/quickwit/client/client.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"net/http"
109
"net/url"
@@ -167,6 +166,21 @@ func (c *baseClientImpl) ExecuteMultisearch(r *MultiSearchRequest) (*MultiSearch
167166

168167
c.logger.Debug("Received multisearch response", "code", res.StatusCode, "status", res.Status, "content-length", res.ContentLength)
169168

169+
if res.StatusCode >= 400 {
170+
qe := QuickwitQueryError{
171+
Key: "msearch",
172+
Status: res.StatusCode,
173+
Message: "Error on multisearch",
174+
ResponseBody: res.Body,
175+
QueryParam: queryParams,
176+
RequestBody: r.Requests,
177+
}
178+
179+
errorPayload, _ := json.Marshal(qe)
180+
c.logger.Error(string(errorPayload))
181+
return nil, fmt.Errorf(string(errorPayload))
182+
}
183+
170184
start := time.Now()
171185
c.logger.Debug("Decoding multisearch json response")
172186

@@ -182,16 +196,6 @@ func (c *baseClientImpl) ExecuteMultisearch(r *MultiSearchRequest) (*MultiSearch
182196

183197
msr.Status = res.StatusCode
184198

185-
if res.StatusCode >= 400 {
186-
jsonResponseBody, _ := json.Marshal(res.Body)
187-
jsonQueryParam, _ := json.Marshal(queryParams)
188-
jsonRequestBody, _ := json.Marshal(r.Requests)
189-
err_msg := "Error on multisearch: statusCode = " + strconv.Itoa(res.StatusCode) + ", responseBody = " + string(jsonResponseBody) + ", queryParam = " + string(jsonQueryParam) + ", requestBody = " + string(jsonRequestBody)
190-
c.logger.Error(err_msg)
191-
192-
return &msr, errors.New(err_msg)
193-
}
194-
195199
return &msr, nil
196200
}
197201

pkg/quickwit/client/models.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package es
22

33
import (
44
"encoding/json"
5+
"io"
56
"time"
67
)
78

@@ -43,6 +44,15 @@ type SearchResponseHits struct {
4344
Hits []map[string]interface{}
4445
}
4546

47+
type QuickwitQueryError struct {
48+
Status int `json:"status"`
49+
Key string `json:"key"`
50+
Message string `json:"message"`
51+
ResponseBody io.ReadCloser `json:"response_body"`
52+
RequestBody []*SearchRequest `json:"request_body"`
53+
QueryParam string `json:"query_param"`
54+
}
55+
4656
// SearchResponse represents a search response
4757
type SearchResponse struct {
4858
Error map[string]interface{} `json:"error"`

pkg/quickwit/data_query.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package quickwit
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"regexp"
67
"strconv"
78
"time"
89

910
"github.com/grafana/grafana-plugin-sdk-go/backend"
10-
1111
es "github.com/quickwit-oss/quickwit-datasource/pkg/quickwit/client"
1212
"github.com/quickwit-oss/quickwit-datasource/pkg/quickwit/simplejson"
1313
)
@@ -28,6 +28,26 @@ var newElasticsearchDataQuery = func(client es.Client, dataQuery []backend.DataQ
2828
}
2929
}
3030

31+
func handleQuickwitErrors(err error) (*backend.QueryDataResponse, error) {
32+
if nil == err {
33+
return nil, nil
34+
}
35+
36+
var payload = err.Error()
37+
var qe es.QuickwitQueryError
38+
unmarshall_err := json.Unmarshal([]byte(payload), &qe)
39+
if unmarshall_err == nil {
40+
return nil, err
41+
}
42+
43+
result := backend.QueryDataResponse{
44+
Responses: backend.Responses{},
45+
}
46+
47+
result.Responses[qe.Key] = backend.ErrDataResponse(backend.Status(qe.Status), payload)
48+
return &result, nil
49+
}
50+
3151
func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
3252
queries, err := parseQuery(e.dataQueries)
3353
if err != nil {
@@ -50,14 +70,13 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
5070
}
5171

5272
res, err := e.client.ExecuteMultisearch(req)
53-
if err != nil {
73+
result, err := handleQuickwitErrors(err)
74+
if result != nil {
75+
return result, nil
76+
} else if err != nil {
5477
return &backend.QueryDataResponse{}, err
5578
}
5679

57-
if res.Status == 404 {
58-
return &backend.QueryDataResponse{}, fmt.Errorf("/_msearch endpoint not found, please check your Quickwit version")
59-
}
60-
6180
return parseResponse(res.Responses, queries, e.client.GetConfiguredFields())
6281
}
6382

pkg/quickwit/error_handling_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func TestErrorAvgMissingField(t *testing.T) {
4545
LogLevelField: "lvl",
4646
}
4747
_, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
48-
require.Error(t, err)
48+
// FIXME: add asserts to also test the result is containing an error with the 400 status code
49+
require.Nil(t, err)
4950
}
5051

5152
func TestErrorAvgMissingFieldNoDetailedErrors(t *testing.T) {
@@ -76,7 +77,8 @@ func TestErrorAvgMissingFieldNoDetailedErrors(t *testing.T) {
7677
LogLevelField: "lvl",
7778
}
7879
_, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
79-
require.Error(t, err)
80+
// FIXME: add asserts to also test the result is containing an error with the 400 status code
81+
require.Nil(t, err)
8082
}
8183

8284
func TestErrorTooManyDateHistogramBuckets(t *testing.T) {
@@ -159,10 +161,6 @@ func TestNonElasticError(t *testing.T) {
159161
LogLevelField: "lvl",
160162
}
161163
_, err := queryDataTestWithResponseCode(query, 403, response, configuredFields)
162-
// FIXME: we should return something better.
163-
// currently it returns the error-message about being unable to decode JSON
164-
// it is not 100% clear what we should return to the browser
165-
// (and what to debug-log for example), we could return
166-
// at least something like "unknown response, http status code 403"
167-
require.ErrorContains(t, err, "invalid character")
164+
// FIXME: add asserts to also test the result is containing an error with the 403 status code
165+
require.Nil(t, err)
168166
}

src/datasource.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ export class QuickwitDataSource
125125
catchError((err) => {
126126
if (err.status === 404) {
127127
return of({ status: 'error', message: 'Index does not exists.' });
128-
}
129-
else if (err.message) {
128+
} else if (err.message) {
130129
return of({ status: 'error', message: err.message });
131130
} else {
132131
return of({ status: 'error', message: err.status });
@@ -487,7 +486,7 @@ export class QuickwitDataSource
487486
const error: DataQueryError = {
488487
message: 'Error during context query. Please check JS console logs.',
489488
status: err.status,
490-
statusText: err.statusText,
489+
statusText: err.message,
491490
};
492491
throw error;
493492
})

0 commit comments

Comments
 (0)