diff --git a/CHANGELOG.md b/CHANGELOG.md index c7d5816df53..a8959c20654 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] ------------------- diff --git a/services/httpd/response_writer.go b/services/httpd/response_writer.go index 11cc5fa5d3f..1d25e318466 100644 --- a/services/httpd/response_writer.go +++ b/services/httpd/response_writer.go @@ -3,6 +3,7 @@ package httpd import ( "encoding/csv" "encoding/json" + "errors" "io" "net/http" "strconv" @@ -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. @@ -124,6 +129,7 @@ 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 { @@ -131,12 +137,17 @@ func (f *jsonFormatter) WriteResponse(w io.Writer, resp Response) (err error) { } 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 }