Skip to content

Commit

Permalink
fix(error): unsupported value: +Inf" error not handled gracefully
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
davidby-influx committed Dec 7, 2020
1 parent 7ab33bb commit ebb1fff
Showing 1 changed file with 16 additions and 5 deletions.
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 ebb1fff

Please sign in to comment.