Skip to content
Myr

fix(recovery): record recovered panics in c.Errors (#4698)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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> ---------

authored by DadaVinqi · committed by GitHub · parent 293ad7e · Browse files · 2 files+50 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • 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>

Diff stats

2 files+50 added−1 removed

  • other:+50 added−1 removed

2 files+50 added−1 removed

modified:recovery_test.go+44 added
3 unmodified lines
45package gin67import (8 "errors"9 "net"10 "net/http"11 "os"12 "strings"
139 unmodified lines
152 assert.Contains(t, out, "net/http: abort Handler")153 assert.NotContains(t, out, "panic recovered")154}155156func TestPanicInHandlerRecordsError(t *testing.T) {157 tests := []struct {158 name string159 recoveredErr any160 expectedErr string161 }{162 {163 name: "string panic",164 recoveredErr: "Oops, Houston, we have a problem",165 expectedErr: "Oops, Houston, we have a problem",166 },167 {168 name: "error panic",169 recoveredErr: errors.New("recovered error"),170 expectedErr: "recovered error",171 },172 }173174 for _, tt := range tests {175 t.Run(tt.name, func(t *testing.T) {176 router := New()177178 var recoveredErrors errorMsgs179 router.Use(func(c *Context) {180 c.Next()181 recoveredErrors = c.Errors182 })183 router.Use(RecoveryWithWriter(nil))184 router.GET("/recovery", func(_ *Context) {185 panic(tt.recoveredErr)186 })187188 w := PerformRequest(router, http.MethodGet, "/recovery")189190 assert.Equal(t, http.StatusInternalServerError, w.Code)191 if assert.Len(t, recoveredErrors, 1) {192 assert.EqualError(t, recoveredErrors[0], tt.expectedErr)193 assert.Equal(t, ErrorTypePrivate, recoveredErrors[0].Type)194 }195 })196 }197}198199func TestCustomRecoveryWithWriter(t *testing.T) {200 errBuffer := new(strings.Builder)201 buffer := new(strings.Builder)202 router := New()
211 unmodified lines
modified:recovery.go+6 added-1 removed
104 unmodified lines
105 }106 return strings.Join(lines, "\r\n")107}108109func defaultHandleRecovery(c *Context, _ any) {109func defaultHandleRecovery(c *Context, err any) {110 e, ok := err.(error)111 if !ok {112 e = fmt.Errorf("%v", err)113 }114 c.Error(e) //nolint: errcheck115 c.AbortWithStatus(http.StatusInternalServerError)116}117118// stack returns a nicely formatted stack frame, skipping skip frames.
86 unmodified lines