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

Only push net error to c.Errors and panic others errors #3587

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,14 @@ func (c *Context) Render(code int, r render.Render) {
}

if err := r.Render(c.Writer); err != nil {
// Pushing error to c.Errors
_ = c.Error(err)
c.Abort()
// if err is net error, pushing error to c.Errors
netOpErr := &net.OpError{}
if ok := errors.As(err, &netOpErr); ok {
_ = c.Error(err)
c.Abort()
} else {
panic(err)
}
}
}

Expand Down
42 changes: 35 additions & 7 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ import (

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

var errTestRender = errors.New("TestRender")
var errTestRender = errors.New("TestPanicRender")

var errNetOpErr = &net.OpError{
Op: "testneterr",
Net: "",
Source: nil,
Addr: nil,
Err: nil,
}

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

type TestRender struct{}
type TestPanicRender struct{}

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

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

func TestContextRenderPanicIfErr(t *testing.T) {
defer func() {
r := recover()
assert.Equal(t, fmt.Sprint(errTestRender), fmt.Sprint(r))
}()

w := httptest.NewRecorder()
c, _ := CreateTestContext(w)

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

type TestNetErrorRender struct{}

func (*TestNetErrorRender) Render(http.ResponseWriter) error {
return errNetOpErr
}

func (*TestNetErrorRender) WriteContentType(http.ResponseWriter) {}

func TestContextRenderIfErr(t *testing.T) {
func TestContextRenderIfNetErr(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)

c.Render(http.StatusOK, &TestRender{})
c.Render(http.StatusOK, &TestNetErrorRender{})

assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors)
assert.Equal(t, errorMsgs{&Error{Err: errNetOpErr, Type: 1}}, c.Errors)
}

// Tests that the response is serialized as JSON
Expand Down