Skip to content

Commit ffe59c8

Browse files
committed
show the original error message returned from the VictoriaLogs backend
fix tests update changelog description update changelog description
1 parent faf9d31 commit ffe59c8

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## tip
44

5+
* BUGFIX: show the original error message returned from the VictoriaLogs backend on status code 400. It should help to troubleshoot problems with query or syntax. See [this pull request](https://github.com/VictoriaMetrics/victorialogs-datasource/pull/287).
6+
57
## v0.16.2
68

79
* BUGFIX: properly close io.ReadCloser to avoid memory leak. See [this pull request](https://github.com/VictoriaMetrics/victorialogs-datasource/pull/280).

pkg/plugin/datasource.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,14 @@ func (d *Datasource) datasourceQuery(ctx context.Context, q *Query, isStream boo
298298
}
299299

300300
if resp.StatusCode != http.StatusOK {
301-
if resp.StatusCode == http.StatusUnprocessableEntity {
301+
switch resp.StatusCode {
302+
case http.StatusUnprocessableEntity:
302303
return nil, parseErrorResponse(resp.Body)
304+
case http.StatusBadRequest:
305+
return nil, parseStringResponseError(resp.Body)
306+
default:
307+
return nil, fmt.Errorf("failed to make http request: %d", resp.StatusCode)
303308
}
304-
return nil, fmt.Errorf("got unexpected response status code: %d", resp.StatusCode)
305309
}
306310

307311
return resp.Body, nil

pkg/plugin/datasource_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestDatasourceQueryRequest(t *testing.T) {
134134
}
135135
}
136136

137-
expErr(ctx, "got unexpected response status code: 500") // 0
137+
expErr(ctx, "failed to make http request: 500") // 0
138138
expErr(ctx, "error get object from decoded response: value doesn't contain object; it contains number") // 1
139139
expErr(ctx, "error parse time from _time field: cannot parse acdf: cannot parse duration \"acdf\"") // 2
140140
expErr(ctx, "error decode response: cannot parse JSON: cannot parse number: unexpected char: \"c\"; unparsed tail: \"cannot parse query []: missing query; context: []\"") // 3
@@ -545,7 +545,7 @@ func TestDatasourceStreamQueryRequest(t *testing.T) {
545545
}
546546
}
547547

548-
expErr(ctx, "got unexpected response status code: 500") // 0
548+
expErr(ctx, "failed to make http request: 500") // 0
549549
expErr(ctx, "error get object from decoded response: value doesn't contain object; it contains number") // 1
550550
expErr(ctx, "error parse time from _time field: cannot parse acdf: cannot parse duration \"acdf\"") // 2
551551
expErr(ctx, "error decode response: cannot parse JSON: cannot parse number: unexpected char: \"c\"; unparsed tail: \"cannot parse query []: missing query; context: []\"") // 3

pkg/plugin/response.go

+12
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ func parseErrorResponse(reader io.Reader) error {
316316
return nil
317317
}
318318

319+
// parseStringResponseError reads data from the reader and returns error
320+
func parseStringResponseError(reader io.Reader) error {
321+
d, err := io.ReadAll(reader)
322+
if err != nil {
323+
return fmt.Errorf("failed to read body response: %w", err)
324+
}
325+
if len(d) == 0 {
326+
return fmt.Errorf("got empty response from the datasource")
327+
}
328+
return fmt.Errorf("error from datasource: %s", string(d))
329+
}
330+
319331
// labelsToJSON converts labels to json representation
320332
// data.Labels when converted to JSON keep the fields sorted
321333
func labelsToJSON(labels data.Labels) (json.RawMessage, error) {

0 commit comments

Comments
 (0)