Merge 11f2321b0224c7cc1cc471fc2b126e6ea709b9da into 43fe48e8a0f44af783116cdb010725e6bb50255f

This commit is contained in:
林桉 2026-10-02 19:10:41 +08:00 committed by GitHub
commit d836ed4076
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 2 deletions

View File

@ -1209,7 +1209,8 @@ 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)
renderErr := c.Error(err)
renderErr.Type |= ErrorTypeRender
c.Abort()
}
}

View File

@ -1164,7 +1164,46 @@ func TestContextRenderIfErr(t *testing.T) {
c.Render(http.StatusOK, &TestRender{})
assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: 1}}, c.Errors)
assert.Equal(t, errorMsgs{&Error{Err: errTestRender, Type: ErrorTypePrivate | ErrorTypeRender}}, c.Errors)
assert.Equal(t, c.Errors, c.Errors.ByType(ErrorTypeRender))
assert.True(t, c.IsAborted())
}
type failingJSONMarshaler struct {
err error
}
func (m failingJSONMarshaler) MarshalJSON() ([]byte, error) {
return nil, m.err
}
func TestContextRenderJSONErrorType(t *testing.T) {
for _, typ := range []ErrorType{ErrorTypePrivate, ErrorTypePublic, ErrorTypeBind | ErrorTypePrivate} {
t.Run(fmt.Sprintf("type_%d", typ), func(t *testing.T) {
original := &Error{Err: errTestRender, Type: typ, Meta: "render metadata"}
var captured *Context
called := false
router := New()
router.GET("/", func(c *Context) {
captured = c
c.JSON(http.StatusOK, failingJSONMarshaler{err: original})
}, func(c *Context) {
called = true
})
router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
require.Len(t, captured.Errors, 1)
assert.Same(t, original, captured.Errors[0])
assert.Equal(t, typ|ErrorTypeRender, captured.Errors[0].Type)
assert.Equal(t, "render metadata", captured.Errors[0].Meta)
require.ErrorIs(t, captured.Errors[0], errTestRender)
assert.Len(t, captured.Errors.ByType(ErrorTypeRender), 1)
assert.Len(t, captured.Errors.ByType(typ), 1)
assert.True(t, captured.IsAborted())
assert.False(t, called)
})
}
}
// Tests that the response is serialized as JSON