Skip to content

Commit

Permalink
Issue #28: move throw error in order to quit fast
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Nov 25, 2023
1 parent a7603bf commit 90be034
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 14 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,20 @@ 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{
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 +195,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
9 changes: 9 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,14 @@ type SearchResponseHits struct {
Hits []map[string]interface{}
}

type QuickwitQueryError struct {
Status int `json:"status"`
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
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 90be034

Please sign in to comment.