Skip to content

Commit

Permalink
issue-126: show full error from the datasource if it response with 42…
Browse files Browse the repository at this point in the history
…2 status code (#128)

* issue-126: show fill error from the datasource if it response with 422 status code

* issue-126: fix CHANGELOG.md
  • Loading branch information
dmitryk-dk authored Dec 3, 2024
1 parent 3ec0861 commit 899e371
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions pkg/plugin/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/plugin/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 899e371

Please sign in to comment.