From ebb1fff9ecb44447e94c20e3129d3a9ed0b34854 Mon Sep 17 00:00:00 2001 From: davidby-influx Date: Thu, 3 Dec 2020 17:15:26 -0800 Subject: [PATCH] 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 https://github.com/influxdata/influxdb/issues/20249 (cherry picked from commit 240707757671f1680838169325adccdd8ef20290) --- services/httpd/response_writer.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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 }