File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed
Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change 11package http
22
33import (
4+ "bytes"
45 "encoding/json"
56 "errors"
67 "net/http"
@@ -53,9 +54,18 @@ func (r Responder) Respond(data any, err error) {
5354 r .w .Header ().Set ("Content-Type" , "application/json" )
5455 }
5556
57+ var buf bytes.Buffer
58+ encoder := json .NewEncoder (& buf )
59+
60+ if err := encoder .Encode (resp ); err != nil {
61+ // JSON encoding failed (e.g. NaN, Inf, unsupported types)
62+ http .Error (r .w , http .StatusText (http .StatusInternalServerError ), http .StatusInternalServerError )
63+ return
64+ }
65+
5666 r .w .WriteHeader (statusCode )
67+ _ , _ = r .w .Write (buf .Bytes ())
5768
58- _ = json .NewEncoder (r .w ).Encode (resp )
5969}
6070
6171// handleSpecialResponseTypes handles special response types that bypass JSON encoding.
You can’t perform that action at this time.
0 commit comments