fix: log panic on Error() or String() panics (#4136)

This commit is contained in:
Kevin Wan 2024-05-10 12:49:34 +08:00 committed by GitHub
parent 74331a45c9
commit 057bae92ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 7 deletions

View File

@ -433,7 +433,7 @@ func encodeWithRecover(arg any, fn func() string) (ret string) {
if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
ret = nilAngleString
} else {
panic(err)
ret = fmt.Sprintf("panic: %v", err)
}
}
}()

View File

@ -348,15 +348,11 @@ func TestStructedLogInfow(t *testing.T) {
})
}
func TestStructedLogInfowNil(t *testing.T) {
func TestStructedLogFieldNil(t *testing.T) {
w := new(mockWriter)
old := writer.Swap(w)
defer writer.Store(old)
assert.Panics(t, func() {
var ps panicStringer
Infow("test", Field("bb", ps))
})
assert.NotPanics(t, func() {
var s *string
Infow("test", Field("bb", s))
@ -365,6 +361,12 @@ func TestStructedLogInfowNil(t *testing.T) {
var e *nilError
Errorw("test", Field("bb", e))
})
assert.NotPanics(t, func() {
var p panicStringer
Infow("test", Field("bb", p))
var ps innerPanicStringer
Infow("test", Field("bb", ps))
})
}
func TestStructedLogInfoConsoleAny(t *testing.T) {
@ -895,7 +897,18 @@ func (s *nilStringer) String() string {
return s.Name
}
type panicStringer struct{}
type innerPanicStringer struct {
Inner *struct {
Name string
}
}
func (s innerPanicStringer) String() string {
return s.Inner.Name
}
type panicStringer struct {
}
func (s panicStringer) String() string {
panic("panic")