Skip to content

Commit

Permalink
server/writer: don't call WriteStatus() twice on encoding errors
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <stephan@styra.com>
  • Loading branch information
srenatus committed Oct 15, 2024
1 parent c140da4 commit cb3ac5a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion server/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func Error(w http.ResponseWriter, status int, err *types.ErrorV1) {

// JSON writes a response with the specified status code and object. The object
// will be JSON serialized.
// Deprecated: This method is problematic when using a non-200 status `code`: if
// encoding the payload fails, it'll print "superfluous call to WriteHeader()"
// logs.
func JSON(w http.ResponseWriter, code int, v interface{}, pretty bool) {
enc := json.NewEncoder(w)
if pretty {
Expand All @@ -74,7 +77,18 @@ func JSON(w http.ResponseWriter, code int, v interface{}, pretty bool) {

// JSONOK is a helper for status "200 OK" responses
func JSONOK(w http.ResponseWriter, v interface{}, pretty bool) {
JSON(w, http.StatusOK, v, pretty)
enc := json.NewEncoder(w)
if pretty {
enc.SetIndent("", " ")
}

w.Header().Add("Content-Type", "application/json")
// If Encode() calls w.Write() for the first time, it'll set the HTTP status
// to 200 OK.
if err := enc.Encode(v); err != nil {
ErrorAuto(w, err)
return
}
}

// Bytes writes a response with the specified status code and bytes.
Expand Down

0 comments on commit cb3ac5a

Please sign in to comment.