Skip to content

Commit

Permalink
Issue #28: handle errors coming from quickwit
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Nov 25, 2023
1 parent a7603bf commit b6bc737
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
26 changes: 15 additions & 11 deletions pkg/quickwit/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -167,6 +166,21 @@ func (c *baseClientImpl) ExecuteMultisearch(r *MultiSearchRequest) (*MultiSearch

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

if res.StatusCode >= 400 {
qe := QuickwitQueryError{
Key: "msearch",
Status: res.StatusCode,
Message: "Error on multisearch",
ResponseBody: res.Body,
QueryParam: queryParams,
RequestBody: r.Requests,
}

errorPayload, _ := json.Marshal(qe)
c.logger.Error(string(errorPayload))
return nil, fmt.Errorf(string(errorPayload))
}

start := time.Now()
c.logger.Debug("Decoding multisearch json response")

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

msr.Status = res.StatusCode

if res.StatusCode >= 400 {
jsonResponseBody, _ := json.Marshal(res.Body)
jsonQueryParam, _ := json.Marshal(queryParams)
jsonRequestBody, _ := json.Marshal(r.Requests)
err_msg := "Error on multisearch: statusCode = " + strconv.Itoa(res.StatusCode) + ", responseBody = " + string(jsonResponseBody) + ", queryParam = " + string(jsonQueryParam) + ", requestBody = " + string(jsonRequestBody)
c.logger.Error(err_msg)

return &msr, errors.New(err_msg)
}

return &msr, nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/quickwit/client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package es

import (
"encoding/json"
"io"
"time"
)

Expand Down Expand Up @@ -43,6 +44,15 @@ type SearchResponseHits struct {
Hits []map[string]interface{}
}

type QuickwitQueryError struct {
Status int `json:"status"`
Key string `json:"key"`
Message string `json:"message"`
ResponseBody io.ReadCloser `json:"response_body"`
RequestBody []*SearchRequest `json:"request_body"`
QueryParam string `json:"query_param"`
}

// SearchResponse represents a search response
type SearchResponse struct {
Error map[string]interface{} `json:"error"`
Expand Down
33 changes: 28 additions & 5 deletions pkg/quickwit/data_query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package quickwit

import (
"encoding/json"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -28,6 +29,29 @@ var newElasticsearchDataQuery = func(client es.Client, dataQuery []backend.DataQ
}
}

func handleQuickwitErrors(err error) (*backend.QueryDataResponse, error) {
if nil == err {
return nil, nil
}

var qe es.QuickwitQueryError
unmarshall_err := json.Unmarshal([]byte(err.Error()), &qe)
if unmarshall_err == nil {
return nil, err
}

result := backend.QueryDataResponse{
Responses: backend.Responses{},
}

result.Responses[qe.Key] = backend.DataResponse{
Error: err,
Status: backend.Status(qe.Status),
}

return &result, nil
}

func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
queries, err := parseQuery(e.dataQueries)
if err != nil {
Expand All @@ -50,14 +74,13 @@ func (e *elasticsearchDataQuery) execute() (*backend.QueryDataResponse, error) {
}

res, err := e.client.ExecuteMultisearch(req)
if err != nil {
result, err := handleQuickwitErrors(err)
if result != nil {
return result, nil
} else if err != nil {
return &backend.QueryDataResponse{}, err
}

if res.Status == 404 {
return &backend.QueryDataResponse{}, fmt.Errorf("/_msearch endpoint not found, please check your Quickwit version")
}

return parseResponse(res.Responses, queries, e.client.GetConfiguredFields())
}

Expand Down
14 changes: 6 additions & 8 deletions pkg/quickwit/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func TestErrorAvgMissingField(t *testing.T) {
LogLevelField: "lvl",
}
_, err := queryDataTestWithResponseCode(query, 400, response, configuredFields)
require.Error(t, err)
// FIXME: add asserts to also test the result is containing an error with the 400 status code
require.Nil(t, err)
}

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

func TestErrorTooManyDateHistogramBuckets(t *testing.T) {
Expand Down Expand Up @@ -159,10 +161,6 @@ func TestNonElasticError(t *testing.T) {
LogLevelField: "lvl",
}
_, err := queryDataTestWithResponseCode(query, 403, response, configuredFields)
// FIXME: we should return something better.
// currently it returns the error-message about being unable to decode JSON
// it is not 100% clear what we should return to the browser
// (and what to debug-log for example), we could return
// at least something like "unknown response, http status code 403"
require.ErrorContains(t, err, "invalid character")
// FIXME: add asserts to also test the result is containing an error with the 403 status code
require.Nil(t, err)
}
5 changes: 2 additions & 3 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ export class QuickwitDataSource
catchError((err) => {
if (err.status === 404) {
return of({ status: 'error', message: 'Index does not exists.' });
}
else if (err.message) {
} else if (err.message) {
return of({ status: 'error', message: err.message });
} else {
return of({ status: 'error', message: err.status });
Expand Down Expand Up @@ -487,7 +486,7 @@ export class QuickwitDataSource
const error: DataQueryError = {
message: 'Error during context query. Please check JS console logs.',
status: err.status,
statusText: err.statusText,
statusText: err.message,
};
throw error;
})
Expand Down

0 comments on commit b6bc737

Please sign in to comment.