mirror of https://github.com/gin-gonic/gin.git
Stop useless panicking in context and render (#2150)
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
153b229fcc
commit
0c96a20209
|
@ -924,7 +924,9 @@ 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)
|
// Pushing error to c.Errors
|
||||||
|
_ = c.Error(err)
|
||||||
|
c.Abort()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,8 @@ import (
|
||||||
|
|
||||||
var _ context.Context = (*Context)(nil)
|
var _ context.Context = (*Context)(nil)
|
||||||
|
|
||||||
|
var errTestRender = errors.New("TestRender")
|
||||||
|
|
||||||
// Unit tests TODO
|
// Unit tests TODO
|
||||||
// func (c *Context) File(filepath string) {
|
// func (c *Context) File(filepath string) {
|
||||||
// func (c *Context) Negotiate(code int, config Negotiate) {
|
// func (c *Context) Negotiate(code int, config Negotiate) {
|
||||||
|
@ -643,25 +645,21 @@ func TestContextBodyAllowedForStatus(t *testing.T) {
|
||||||
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))
|
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))
|
||||||
}
|
}
|
||||||
|
|
||||||
type TestPanicRender struct{}
|
type TestRender struct{}
|
||||||
|
|
||||||
func (*TestPanicRender) Render(http.ResponseWriter) error {
|
func (*TestRender) Render(http.ResponseWriter) error {
|
||||||
return errors.New("TestPanicRender")
|
return errTestRender
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*TestPanicRender) WriteContentType(http.ResponseWriter) {}
|
func (*TestRender) WriteContentType(http.ResponseWriter) {}
|
||||||
|
|
||||||
func TestContextRenderPanicIfErr(t *testing.T) {
|
func TestContextRenderIfErr(t *testing.T) {
|
||||||
defer func() {
|
|
||||||
r := recover()
|
|
||||||
assert.Equal(t, "TestPanicRender", fmt.Sprint(r))
|
|
||||||
}()
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
c.Render(http.StatusOK, &TestPanicRender{})
|
c.Render(http.StatusOK, &TestRender{})
|
||||||
|
|
||||||
assert.Fail(t, "Panic not detected")
|
assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that the response is serialized as JSON
|
// Tests that the response is serialized as JSON
|
||||||
|
|
|
@ -53,11 +53,8 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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) error {
|
||||||
if err = WriteJSON(w, r.Data); err != nil {
|
return WriteJSON(w, r.Data)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteContentType (JSON) writes JSON ContentType.
|
// WriteContentType (JSON) writes JSON ContentType.
|
||||||
|
|
|
@ -40,12 +40,12 @@ func TestRenderJSON(t *testing.T) {
|
||||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderJSONPanics(t *testing.T) {
|
func TestRenderJSONError(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
data := make(chan int)
|
data := make(chan int)
|
||||||
|
|
||||||
// json: unsupported type: chan int
|
// json: unsupported type: chan int
|
||||||
assert.Panics(t, func() { assert.NoError(t, (JSON{data}).Render(w)) })
|
assert.Error(t, (JSON{data}).Render(w))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderIndentedJSON(t *testing.T) {
|
func TestRenderIndentedJSON(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue