server/writer: don't call WriteStatus() twice on encoding errors

Signed-off-by: Stephan Renatus <stephan@styra.com>
This commit is contained in:
Stephan Renatus
2024-10-11 13:48:18 +02:00
parent c140da4c2e
commit cb3ac5a838
+15 -1
View File
@@ -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 {
@@ -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.