Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop useless panicking in context and render #2150

Merged
merged 16 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,9 @@ func (c *Context) Render(code int, r render.Render) {
}

if err := r.Render(c.Writer); err != nil {
panic(err)
// Pushing error to c.Errors
_ = c.Error(err)
c.Abort()
}
}

Expand Down
20 changes: 9 additions & 11 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (

var _ context.Context = (*Context)(nil)

var errTestRender = errors.New("TestRender")

// Unit tests TODO
// func (c *Context) File(filepath string) {
// func (c *Context) Negotiate(code int, config Negotiate) {
Expand Down Expand Up @@ -643,25 +645,21 @@ func TestContextBodyAllowedForStatus(t *testing.T) {
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError))
}

type TestPanicRender struct{}
type TestRender struct{}

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

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

func TestContextRenderPanicIfErr(t *testing.T) {
defer func() {
r := recover()
assert.Equal(t, "TestPanicRender", fmt.Sprint(r))
}()
func TestContextRenderIfErr(t *testing.T) {
w := httptest.NewRecorder()
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
Expand Down
7 changes: 2 additions & 5 deletions render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ 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
func (r JSON) Render(w http.ResponseWriter) error {
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 @@ -40,12 +40,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