mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
fix: log panic on Error() or String() panics (#4136)
This commit is contained in:
parent
74331a45c9
commit
057bae92ab
@ -433,7 +433,7 @@ func encodeWithRecover(arg any, fn func() string) (ret string) {
|
|||||||
if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
|
if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
|
||||||
ret = nilAngleString
|
ret = nilAngleString
|
||||||
} else {
|
} else {
|
||||||
panic(err)
|
ret = fmt.Sprintf("panic: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -348,15 +348,11 @@ func TestStructedLogInfow(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStructedLogInfowNil(t *testing.T) {
|
func TestStructedLogFieldNil(t *testing.T) {
|
||||||
w := new(mockWriter)
|
w := new(mockWriter)
|
||||||
old := writer.Swap(w)
|
old := writer.Swap(w)
|
||||||
defer writer.Store(old)
|
defer writer.Store(old)
|
||||||
|
|
||||||
assert.Panics(t, func() {
|
|
||||||
var ps panicStringer
|
|
||||||
Infow("test", Field("bb", ps))
|
|
||||||
})
|
|
||||||
assert.NotPanics(t, func() {
|
assert.NotPanics(t, func() {
|
||||||
var s *string
|
var s *string
|
||||||
Infow("test", Field("bb", s))
|
Infow("test", Field("bb", s))
|
||||||
@ -365,6 +361,12 @@ func TestStructedLogInfowNil(t *testing.T) {
|
|||||||
var e *nilError
|
var e *nilError
|
||||||
Errorw("test", Field("bb", e))
|
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) {
|
func TestStructedLogInfoConsoleAny(t *testing.T) {
|
||||||
@ -895,7 +897,18 @@ func (s *nilStringer) String() string {
|
|||||||
return s.Name
|
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 {
|
func (s panicStringer) String() string {
|
||||||
panic("panic")
|
panic("panic")
|
||||||
|
Loading…
Reference in New Issue
Block a user