Skip to content

Commit 899e371

Browse files
authored
issue-126: show full error from the datasource if it response with 422 status code (#128)
* issue-126: show fill error from the datasource if it response with 422 status code * issue-126: fix CHANGELOG.md
1 parent 3ec0861 commit 899e371

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* FEATURE: Add support for the `$__range` variable in queries. It will be transformed to the `[time_from, time_to]` in the Unix format. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/112).
66

7+
* BUGFIX: show the original error message returned from the VictoriaLogs backend. It should help to troubleshoot problems with query or syntax. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/126).
8+
79
## v0.8.0
810

911
* FEATURE: add support for the `/select/logsql/stats_query` and `/select/logsql/stats_query_range` API calls. This feature helps to build different panels with statistic data. See [this issue](https://github.com/VictoriaMetrics/victorialogs-datasource/issues/61).

pkg/plugin/datasource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ func (d *Datasource) datasourceQuery(ctx context.Context, q *Query, isStream boo
220220
}
221221

222222
if resp.StatusCode != http.StatusOK {
223+
if resp.StatusCode == http.StatusUnprocessableEntity {
224+
return nil, parseErrorResponse(resp.Body)
225+
}
223226
return nil, fmt.Errorf("got unexpected response status code: %d", resp.StatusCode)
224227
}
225228

pkg/plugin/response.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ func parseStatsResponse(reader io.Reader, q *Query) backend.DataResponse {
288288
return backend.DataResponse{Frames: frames}
289289
}
290290

291+
// parseErrorResponse reads data from the reader and returns error
292+
func parseErrorResponse(reader io.Reader) error {
293+
var rs Response
294+
if err := json.NewDecoder(reader).Decode(&rs); err != nil {
295+
err = fmt.Errorf("failed to decode body response: %w", err)
296+
return err
297+
}
298+
299+
if rs.Status == "error" {
300+
return fmt.Errorf("error: %s", rs.Error)
301+
}
302+
303+
if rs.Error == "" {
304+
return fmt.Errorf("got unexpected error from the datasource")
305+
}
306+
307+
return nil
308+
}
309+
291310
// labelsToJSON converts labels to json representation
292311
// data.Labels when converted to JSON keep the fields sorted
293312
func labelsToJSON(labels data.Labels) (json.RawMessage, error) {
@@ -328,6 +347,7 @@ type Data struct {
328347
type Response struct {
329348
Status string `json:"status"`
330349
Data Data `json:"data"`
350+
Error string `json:"error"`
331351
}
332352

333353
// logStats represents response result from the

0 commit comments

Comments
 (0)