Removed useless panicking from renderer/context

This commit is contained in:
eeonevision 2019-11-25 11:33:12 +03:00
parent 231ff00d1f
commit 0709ae3b10
2 changed files with 8 additions and 13 deletions

View File

@ -16,6 +16,7 @@ import (
"net/url" "net/url"
"os" "os"
"strings" "strings"
"syscall"
"time" "time"
"github.com/gin-contrib/sse" "github.com/gin-contrib/sse"
@ -814,7 +815,11 @@ func (c *Context) Render(code int, r render.Render) {
} }
if err := r.Render(c.Writer); err != nil { if err := r.Render(c.Writer); err != nil {
panic(err) c.Error(err)
if err == syscall.EPIPE {
c.AbortWithStatus(httpStatusClientClosedRequest)
}
} }
} }

View File

@ -54,10 +54,8 @@ var jsonAsciiContentType = []string{"application/json"}
// Render (JSON) writes data with custom ContentType. // Render (JSON) writes data with custom ContentType.
func (r JSON) Render(w http.ResponseWriter) (err error) { func (r JSON) Render(w http.ResponseWriter) (err error) {
if err = WriteJSON(w, r.Data); err != nil { r.WriteContentType(w)
panic(err) return json.NewEncoder(w).Encode(r.Data)
}
return
} }
// WriteContentType (JSON) writes JSON ContentType. // WriteContentType (JSON) writes JSON ContentType.
@ -65,14 +63,6 @@ func (r JSON) WriteContentType(w http.ResponseWriter) {
writeContentType(w, jsonContentType) writeContentType(w, jsonContentType)
} }
// WriteJSON marshals the given interface object and writes it with custom ContentType.
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
writeContentType(w, jsonContentType)
encoder := json.NewEncoder(w)
err := encoder.Encode(&obj)
return err
}
// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType. // Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
func (r IndentedJSON) Render(w http.ResponseWriter) error { func (r IndentedJSON) Render(w http.ResponseWriter) error {
r.WriteContentType(w) r.WriteContentType(w)