Skip to content

Commit

Permalink
fix: withRecover cannot recover panic (#1169)
Browse files Browse the repository at this point in the history
* fix: withRecover cannot recover panic

* fix: withRecover cannot recover panic
  • Loading branch information
wanyuqin authored Oct 15, 2024
1 parent 9462ea8 commit ac4e3d6
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions primitive/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(interface{}) {
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)
}
}
}
}()
Expand Down

0 comments on commit ac4e3d6

Please sign in to comment.