fix: log panic when use nil error or stringer with Field method (#4130)

This commit is contained in:
Kevin Wan
2024-05-10 00:31:36 +08:00
committed by GitHub
parent 9d551d507f
commit 74331a45c9
4 changed files with 88 additions and 5 deletions

View File

@@ -348,6 +348,25 @@ func TestStructedLogInfow(t *testing.T) {
})
}
func TestStructedLogInfowNil(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))
var d *nilStringer
Infow("test", Field("bb", d))
var e *nilError
Errorw("test", Field("bb", e))
})
}
func TestStructedLogInfoConsoleAny(t *testing.T) {
w := new(mockWriter)
old := writer.Swap(w)
@@ -859,3 +878,25 @@ func validateFields(t *testing.T, content string, fields map[string]any) {
}
}
}
type nilError struct {
Name string
}
func (e *nilError) Error() string {
return e.Name
}
type nilStringer struct {
Name string
}
func (s *nilStringer) String() string {
return s.Name
}
type panicStringer struct{}
func (s panicStringer) String() string {
panic("panic")
}