Stop useless panicking in context and render (#2150)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Vladislav Dmitriyev · committed by GitHub · parent 153b229 · Browse files · 4 files·+16 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Diff stats
4 files+16 added−19 removed
- other:+16 added−19 removed
Provenance
- Commit
0c96a20- Tree
fe69d1c- Author
Vladislav Dmitriyev <d7vladislav@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
153b229
4 files changed+16 added−19 removed
modified:render/json.go+2 added-5 removed
51 unmodified lines52 jsonASCIIContentType = []string{"application/json"}53 )54 55 // Render (JSON) writes data with custom ContentType.56-func (r JSON) Render(w http.ResponseWriter) (err error) {57- if err = WriteJSON(w, r.Data); err != nil {58- panic(err)59- }60- return56+func (r JSON) Render(w http.ResponseWriter) error {57+ return WriteJSON(w, r.Data)58 }59 60 // WriteContentType (JSON) writes JSON ContentType.61 func (r JSON) WriteContentType(w http.ResponseWriter) {129 unmodified linesmodified:render/render_test.go+2 added-2 removed
38 unmodified lines39 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())40 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))41 }42 43-func TestRenderJSONPanics(t *testing.T) {43+func TestRenderJSONError(t *testing.T) {44 w := httptest.NewRecorder()45 data := make(chan int)46 47 // json: unsupported type: chan int48- assert.Panics(t, func() { assert.NoError(t, (JSON{data}).Render(w)) })48+ assert.Error(t, (JSON{data}).Render(w))49 }50 51 func TestRenderIndentedJSON(t *testing.T) {52 w := httptest.NewRecorder()482 unmodified linesmodified:context_test.go+9 added-11 removed
30 unmodified lines31 )32 33 var _ context.Context = (*Context)(nil)34 35+var errTestRender = errors.New("TestRender")36+37 // Unit tests TODO38 // func (c *Context) File(filepath string) {39 // func (c *Context) Negotiate(code int, config Negotiate) {40 // BAD case: func (c *Context) Render(code int, render render.Render, obj ...interface{}) {603 unmodified lines644 assert.False(t, false, bodyAllowedForStatus(http.StatusNotModified))645 assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))646 }647 646-type TestPanicRender struct{}648+type TestRender struct{}649 648-func (*TestPanicRender) Render(http.ResponseWriter) error {649- return errors.New("TestPanicRender")650+func (*TestRender) Render(http.ResponseWriter) error {651+ return errTestRender652 }653 652-func (*TestPanicRender) WriteContentType(http.ResponseWriter) {}654+func (*TestRender) WriteContentType(http.ResponseWriter) {}655 654-func TestContextRenderPanicIfErr(t *testing.T) {655- defer func() {656- r := recover()657- assert.Equal(t, "TestPanicRender", fmt.Sprint(r))658- }()656+func TestContextRenderIfErr(t *testing.T) {657 w := httptest.NewRecorder()658 c, _ := CreateTestContext(w)659 662- c.Render(http.StatusOK, &TestPanicRender{})660+ c.Render(http.StatusOK, &TestRender{})661 664- assert.Fail(t, "Panic not detected")662+ assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors)663 }664 665 // Tests that the response is serialized as JSON666 // and Content-Type is set to application/json1710 unmodified linesmodified:context.go+3 added-1 removed
922 unmodified lines923 return924 }925 926 if err := r.Render(c.Writer); err != nil {927- panic(err)927+ // Pushing error to c.Errors928+ _ = c.Error(err)929+ c.Abort()930 }931 }932 933 // HTML renders the HTTP template specified by its file name.287 unmodified lines