diff --git a/CHANGELOG.md b/CHANGELOG.md index 5268e30..ab0227c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * 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). +* 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). + ## v0.8.0 * 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). diff --git a/pkg/plugin/datasource.go b/pkg/plugin/datasource.go index 52e0f4c..9ee7bce 100644 --- a/pkg/plugin/datasource.go +++ b/pkg/plugin/datasource.go @@ -220,6 +220,9 @@ func (d *Datasource) datasourceQuery(ctx context.Context, q *Query, isStream boo } if resp.StatusCode != http.StatusOK { + if resp.StatusCode == http.StatusUnprocessableEntity { + return nil, parseErrorResponse(resp.Body) + } return nil, fmt.Errorf("got unexpected response status code: %d", resp.StatusCode) } diff --git a/pkg/plugin/response.go b/pkg/plugin/response.go index 415dd29..4bdc09d 100644 --- a/pkg/plugin/response.go +++ b/pkg/plugin/response.go @@ -288,6 +288,25 @@ func parseStatsResponse(reader io.Reader, q *Query) backend.DataResponse { return backend.DataResponse{Frames: frames} } +// parseErrorResponse reads data from the reader and returns error +func parseErrorResponse(reader io.Reader) error { + var rs Response + if err := json.NewDecoder(reader).Decode(&rs); err != nil { + err = fmt.Errorf("failed to decode body response: %w", err) + return err + } + + if rs.Status == "error" { + return fmt.Errorf("error: %s", rs.Error) + } + + if rs.Error == "" { + return fmt.Errorf("got unexpected error from the datasource") + } + + return nil +} + // labelsToJSON converts labels to json representation // data.Labels when converted to JSON keep the fields sorted func labelsToJSON(labels data.Labels) (json.RawMessage, error) { @@ -328,6 +347,7 @@ type Data struct { type Response struct { Status string `json:"status"` Data Data `json:"data"` + Error string `json:"error"` } // logStats represents response result from the