Skip to content

Commit dc51b84

Browse files
committed
Issue #4: error handling
1 parent ae3d633 commit dc51b84

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

pkg/quickwit/quickwit.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"net/http"
@@ -37,6 +38,19 @@ type QuickwitMapping struct {
3738
} `json:"index_config"`
3839
}
3940

41+
type QuickwitCreationErrorPayload struct {
42+
Message string `json:"message"`
43+
StatusCode int `json:"status"`
44+
}
45+
46+
func marshallErrorAsException(statusCode int, message string) error {
47+
var payload QuickwitCreationErrorPayload
48+
payload.Message = message
49+
payload.StatusCode = statusCode
50+
json, _ := json.Marshal(payload)
51+
return errors.New(string(json))
52+
}
53+
4054
func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (string, string, error) {
4155
mappingEndpointUrl := qwUrl + "/indexes/" + index
4256
qwlog.Info("Calling quickwit endpoint: " + mappingEndpointUrl)
@@ -48,26 +62,27 @@ func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (strin
4862
}
4963

5064
statusCode := r.StatusCode
65+
5166
if statusCode < 200 || statusCode >= 400 {
52-
errMsg := fmt.Sprintf("Error when calling url = %s: statusCode = %d", mappingEndpointUrl, statusCode)
67+
errMsg := fmt.Sprintf("Error when calling url = %s", mappingEndpointUrl)
5368
qwlog.Error(errMsg)
54-
return "", "", fmt.Errorf(errMsg)
69+
return "", "", marshallErrorAsException(statusCode, errMsg)
5570
}
5671

5772
defer r.Body.Close()
5873
body, err := io.ReadAll(r.Body)
5974
if err != nil {
6075
errMsg := fmt.Sprintf("Error when calling url = %s: err = %s", mappingEndpointUrl, err.Error())
6176
qwlog.Error(errMsg)
62-
return "", "", err
77+
return "", "", marshallErrorAsException(statusCode, errMsg)
6378
}
6479

6580
var payload QuickwitMapping
6681
err = json.Unmarshal(body, &payload)
6782
if err != nil {
6883
errMsg := fmt.Sprintf("Unmarshalling body error: err = %s, body = %s", err.Error(), (body))
6984
qwlog.Error(errMsg)
70-
return "", "", fmt.Errorf(errMsg)
85+
return "", "", marshallErrorAsException(statusCode, errMsg)
7186
}
7287

7388
timestampFieldName := payload.IndexConfig.DocMapping.TimestampField
@@ -82,7 +97,7 @@ func getTimestampFieldInfos(index string, qwUrl string, cli *http.Client) (strin
8297
if timestampFieldFormat == "undef" {
8398
errMsg := fmt.Sprintf("No format found for field: %s", string(timestampFieldName))
8499
qwlog.Error(errMsg)
85-
return timestampFieldName, "", fmt.Errorf(errMsg)
100+
return timestampFieldName, "", marshallErrorAsException(statusCode, errMsg)
86101
}
87102

88103
qwlog.Info(fmt.Sprintf("Found timestampFieldName = %s, timestampFieldFormat = %s", timestampFieldName, timestampFieldFormat))

src/datasource.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { bucketAggregationConfig } from 'components/QueryEditor/BucketAggregatio
5151
import { isBucketAggregationWithField } from 'components/QueryEditor/BucketAggregationsEditor/aggregations';
5252
import ElasticsearchLanguageProvider from 'LanguageProvider';
5353
import { ReactNode } from 'react';
54+
import { extractJsonPayload } from 'utils';
5455

5556
export const REF_ID_STARTER_LOG_VOLUME = 'log-volume-';
5657

@@ -91,8 +92,18 @@ export class QuickwitDataSource
9192
let timestampField = fields.find((field) => field.json_path === timestampFieldName);
9293
let timestampFormat = timestampField ? timestampField.field_mapping.output_format || '' : ''
9394
let timestampFieldInfos = { 'field': timestampFieldName, 'format': timestampFormat }
94-
console.log("timestampFieldInfos = " + JSON.stringify(timestampFieldInfos))
9595
return timestampFieldInfos
96+
}),
97+
catchError((err) => {
98+
if (!err.data || !err.data.error) {
99+
let err_source = extractJsonPayload(err.data.error)
100+
if(!err_source) {
101+
throw err
102+
}
103+
}
104+
105+
// the error will be handle in the testDatasource function
106+
return of({'field': '', 'format': ''})
96107
})
97108
).subscribe(result => {
98109
this.timeField = result.field;
@@ -143,7 +154,14 @@ export class QuickwitDataSource
143154
return of({ status: 'success', message: `Index OK. Time field name OK` });
144155
}),
145156
catchError((err) => {
146-
if (err.status === 404) {
157+
if (err.data && err.data.error) {
158+
let err_source = extractJsonPayload(err.data.error)
159+
if (err_source) {
160+
err = err_source
161+
}
162+
}
163+
164+
if (err.status && err.status === 404) {
147165
return of({ status: 'error', message: 'Index does not exists.' });
148166
} else if (err.message) {
149167
return of({ status: 'error', message: err.message });
@@ -377,7 +395,7 @@ export class QuickwitDataSource
377395
return _map(filteredFields, (field) => {
378396
return {
379397
text: field.json_path,
380-
value: typeMap[field.field_mapping.type],
398+
value: typeMap[field.field_mapping.type]
381399
};
382400
});
383401
})

src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ export const describeMetric = (metric: MetricAggregation) => {
1313
return `${metricAggregationConfig[metric.type].label} ${metric.field}`;
1414
};
1515

16+
export const extractJsonPayload = (msg: string) => {
17+
const match = msg.match(/{.*}/);
18+
19+
if (!match) {
20+
return null;
21+
}
22+
23+
try {
24+
return JSON.parse(match[0]);
25+
} catch (error) {
26+
return null;
27+
}
28+
}
29+
1630
/**
1731
* Utility function to clean up aggregations settings objects.
1832
* It removes nullish values and empty strings, array and objects

0 commit comments

Comments
 (0)