mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-08 11:41:09 +08:00
fix(recovery): record recovered panics in c.Errors (#4698)
* fix: record recovered panic errors * chore(deps): bump quic-go to v0.59.1 * refactor(recovery): simplify panic error recording - Collapse the if/else into a single c.Error call in defaultHandleRecovery - Assert the recorded panic error is ErrorTypePrivate in the test Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: m0_66095053 <2876430886@qq.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
293ad7edeb
commit
4a3eb31fb1
@ -106,7 +106,12 @@ func secureRequestDump(r *http.Request) string {
|
|||||||
return strings.Join(lines, "\r\n")
|
return strings.Join(lines, "\r\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultHandleRecovery(c *Context, _ any) {
|
func defaultHandleRecovery(c *Context, err any) {
|
||||||
|
e, ok := err.(error)
|
||||||
|
if !ok {
|
||||||
|
e = fmt.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
c.Error(e) //nolint: errcheck
|
||||||
c.AbortWithStatus(http.StatusInternalServerError)
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -152,6 +153,49 @@ func TestPanicWithAbortHandler(t *testing.T) {
|
|||||||
assert.NotContains(t, out, "panic recovered")
|
assert.NotContains(t, out, "panic recovered")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPanicInHandlerRecordsError(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
recoveredErr any
|
||||||
|
expectedErr string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "string panic",
|
||||||
|
recoveredErr: "Oops, Houston, we have a problem",
|
||||||
|
expectedErr: "Oops, Houston, we have a problem",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "error panic",
|
||||||
|
recoveredErr: errors.New("recovered error"),
|
||||||
|
expectedErr: "recovered error",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
|
||||||
|
var recoveredErrors errorMsgs
|
||||||
|
router.Use(func(c *Context) {
|
||||||
|
c.Next()
|
||||||
|
recoveredErrors = c.Errors
|
||||||
|
})
|
||||||
|
router.Use(RecoveryWithWriter(nil))
|
||||||
|
router.GET("/recovery", func(_ *Context) {
|
||||||
|
panic(tt.recoveredErr)
|
||||||
|
})
|
||||||
|
|
||||||
|
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
|
if assert.Len(t, recoveredErrors, 1) {
|
||||||
|
assert.EqualError(t, recoveredErrors[0], tt.expectedErr)
|
||||||
|
assert.Equal(t, ErrorTypePrivate, recoveredErrors[0].Type)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCustomRecoveryWithWriter(t *testing.T) {
|
func TestCustomRecoveryWithWriter(t *testing.T) {
|
||||||
errBuffer := new(strings.Builder)
|
errBuffer := new(strings.Builder)
|
||||||
buffer := new(strings.Builder)
|
buffer := new(strings.Builder)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user