Skip to content

Commit

Permalink
return err instead of panic in JSON Render
Browse files Browse the repository at this point in the history
  • Loading branch information
Tevic committed Jan 26, 2022
1 parent 580e7da commit e66c446
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
8 changes: 3 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,18 +884,16 @@ func (c *Context) Cookie(name string) (string, error) {
}

// Render writes the response headers and calls render.Render to render data.
func (c *Context) Render(code int, r render.Render) {
func (c *Context) Render(code int, r render.Render) error {
c.Status(code)

if !bodyAllowedForStatus(code) {
r.WriteContentType(c.Writer)
c.Writer.WriteHeaderNow()
return
return nil
}

if err := r.Render(c.Writer); err != nil {
panic(err)
}
return r.Render(c.Writer)
}

// HTML renders the HTTP template specified by its file name.
Expand Down
17 changes: 5 additions & 12 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,25 +642,18 @@ func TestContextBodyAllowedForStatus(t *testing.T) {
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))
}

type TestPanicRender struct{}
type TestErrorRender struct{}

func (*TestPanicRender) Render(http.ResponseWriter) error {
return errors.New("TestPanicRender")
func (*TestErrorRender) Render(http.ResponseWriter) error {
return errors.New("TestErrorRender")
}

func (*TestPanicRender) WriteContentType(http.ResponseWriter) {}
func (*TestErrorRender) WriteContentType(http.ResponseWriter) {}

func TestContextRenderPanicIfErr(t *testing.T) {
defer func() {
r := recover()
assert.Equal(t, "TestPanicRender", fmt.Sprint(r))
}()
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)

c.Render(http.StatusOK, &TestPanicRender{})

assert.Fail(t, "Panic not detected")
assert.Error(t, c.Render(http.StatusOK, &TestErrorRender{}))
}

// Tests that the response is serialized as JSON
Expand Down
5 changes: 1 addition & 4 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ var (

// Render (JSON) writes data with custom ContentType.
func (r JSON) Render(w http.ResponseWriter) (err error) {
if err = WriteJSON(w, r.Data); err != nil {
panic(err)
}
return
return WriteJSON(w, r.Data)
}

// WriteContentType (JSON) writes JSON ContentType.
Expand Down
4 changes: 2 additions & 2 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func TestRenderJSON(t *testing.T) {
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()
data := make(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) {
Expand Down

0 comments on commit e66c446

Please sign in to comment.