Skip to content

Commit

Permalink
fix(error): unsupported value: +Inf" error not handled gracefully (#2…
Browse files Browse the repository at this point in the history
…0276)

* fix(error): unsupported value: +Inf" error not handled gracefully

JSON marshalling errors should be returned properly formatted in JSON
like other errors. This fix formats marshalling errors the same way
influxdb formats other query errors.

Fixes #20249

(cherry picked from commit 2407077)

* chore(changelog): update CHANGELOG.md for PR #20276
  • Loading branch information
davidby-influx authored Dec 7, 2020
1 parent 7ab33bb commit 0212d3c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ v1.8.4 [unreleased]

- [#20101](https://github.com/influxdata/influxdb/pull/20101): fix(write): Successful writes increment write error statistics incorrectly
- [#19696](https://github.com/influxdata/influxdb/pull/19697): fix(flux): add durations to Flux logging
[#20276](https://github.com/influxdata/influxdb/pull/20276): fix(error): unsupported value: +Inf" error not handled gracefully

v1.8.3 [2020-09-30]
-------------------
Expand Down
21 changes: 16 additions & 5 deletions services/httpd/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpd
import (
"encoding/csv"
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -97,9 +98,13 @@ func (w *bytesCountWriter) Write(data []byte) (int, error) {

// WriteResponse writes the response using the formatter.
func (w *responseWriter) WriteResponse(resp Response) (int, error) {
n := 0
writer := bytesCountWriter{w: w.ResponseWriter}
err := w.formatter.WriteResponse(&writer, resp)
return writer.n, err
if err != nil {
n, _ = WriteError(w, err)
}
return writer.n + n, err
}

// Flush flushes the ResponseWriter if it has a Flush() method.
Expand All @@ -124,19 +129,25 @@ type jsonFormatter struct {

func (f *jsonFormatter) WriteResponse(w io.Writer, resp Response) (err error) {
var b []byte

if f.Pretty {
b, err = json.MarshalIndent(resp, "", " ")
} else {
b, err = json.Marshal(resp)
}

if err != nil {
_, err = io.WriteString(w, err.Error())
} else {
_, err = w.Write(b)
err = unnestError(err)
} else if _, err = w.Write(b); err == nil {
_, err = w.Write([]byte("\n"))
}
return
}

w.Write([]byte("\n"))
func unnestError(err error) error {
for errNested := err; errNested != nil; errNested = errors.Unwrap(err) {
err = errNested
}
return err
}

Expand Down

0 comments on commit 0212d3c

Please sign in to comment.