mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 01:40:00 +08:00
fix: log panic when use nil error or stringer with Field method (#4130)
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user