Skip to content

Commit

Permalink
fix: No infinite recursion on write error
Browse files Browse the repository at this point in the history
If there is some error writing to the response writer, we
would previous have infinite recursion.

Re-closes influxdata#20249
  • Loading branch information
lesam committed Mar 4, 2021
1 parent b92e913 commit 5b1bc3d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions services/httpd/response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"strconv"
"errors"
"time"

"github.com/influxdata/influxdb/models"
Expand Down Expand Up @@ -131,15 +132,27 @@ func (f *jsonFormatter) WriteResponse(w io.Writer, resp Response) (err error) {
}

if err != nil {
_, err = io.WriteString(w, err.Error())
unnestedErr := unnestError(err)
// ignore error when already trying to report another error
b2, _ := json.Marshal(unnestedErr.Error())
io.WriteString(w, `{"error":`)
w.Write(b2)
io.WriteString(w, "}")
} else {
_, err = w.Write(b)
}

w.Write([]byte("\n"))
return err
}

func unnestError(err error) error {
for errNested := err; errNested != nil; errNested = errors.Unwrap(err) {
err = errNested
}
return err
}


type csvFormatter struct {
statementID int
columns []string
Expand Down

0 comments on commit 5b1bc3d

Please sign in to comment.