From 1a3aababce5afb8725e29452fd76c25f15c17d60 Mon Sep 17 00:00:00 2001 From: EthanLeo Date: Sun, 22 Sep 2024 22:08:58 +0800 Subject: [PATCH 1/2] fix: withRecover cannot recover panic --- primitive/base.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/primitive/base.go b/primitive/base.go index a45fbb96..61509ead 100644 --- a/primitive/base.go +++ b/primitive/base.go @@ -85,14 +85,22 @@ func verifyIP(ip string) error { return nil } -var PanicHandler func(interface{}) +type PanicHandler func(interface{}) -func WithRecover(fn func()) { +func DefaultPanicHandler(any) { + return +} + +func WithRecover(fn func(), handlers ...PanicHandler) { defer func() { - handler := PanicHandler - if handler != nil { - if err := recover(); err != nil { - handler(err) + if len(handlers) == 0 { + handlers = append(handlers, DefaultPanicHandler) + } + for _, handler := range handlers { + if handler != nil { + if err := recover(); err != nil { + handler(err) + } } } }() From 85d50027afbc92926f7721738b64a6e1d1956bb6 Mon Sep 17 00:00:00 2001 From: EthanLeo Date: Sun, 22 Sep 2024 22:19:26 +0800 Subject: [PATCH 2/2] fix: withRecover cannot recover panic --- primitive/base.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitive/base.go b/primitive/base.go index 61509ead..0042785d 100644 --- a/primitive/base.go +++ b/primitive/base.go @@ -87,7 +87,7 @@ func verifyIP(ip string) error { type PanicHandler func(interface{}) -func DefaultPanicHandler(any) { +func DefaultPanicHandler(interface{}) { return }