Merge ce03fdda7d9a13288eb16721619d4a97f7983a1b into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d

This commit is contained in:
cyhone 2025-03-22 06:32:49 +00:00 committed by GitHub
commit 9073f2e42c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 10 deletions

View File

@ -1051,9 +1051,14 @@ 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 {
// Pushing error to c.Errors // if err is net error, pushing error to c.Errors
_ = c.Error(err) netOpErr := &net.OpError{}
c.Abort() if ok := errors.As(err, &netOpErr); ok {
_ = c.Error(err)
c.Abort()
} else {
panic(err)
}
} }
} }

View File

@ -36,7 +36,15 @@ import (
var _ context.Context = (*Context)(nil) 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 // Unit tests TODO
// func (c *Context) File(filepath string) { // func (c *Context) File(filepath string) {
@ -891,21 +899,41 @@ func TestContextBodyAllowedForStatus(t *testing.T) {
assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError)) 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 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))
}()
func TestContextRenderIfErr(t *testing.T) {
w := httptest.NewRecorder() w := httptest.NewRecorder()
c, _ := CreateTestContext(w) c, _ := CreateTestContext(w)
c.Render(http.StatusOK, &TestRender{}) c.Render(http.StatusOK, &TestPanicRender{})
}
assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors) type TestNetErrorRender struct{}
func (*TestNetErrorRender) Render(http.ResponseWriter) error {
return errNetOpErr
}
func (*TestNetErrorRender) WriteContentType(http.ResponseWriter) {}
func TestContextRenderIfNetErr(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Render(http.StatusOK, &TestNetErrorRender{})
assert.Equal(t, errorMsgs{&Error{Err: errNetOpErr, Type: 1}}, c.Errors)
} }
// Tests that the response is serialized as JSON // Tests that the response is serialized as JSON