initial import
This commit is contained in:
13
core/rescue/recover.go
Normal file
13
core/rescue/recover.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package rescue
|
||||
|
||||
import "zero/core/logx"
|
||||
|
||||
func Recover(cleanups ...func()) {
|
||||
for _, cleanup := range cleanups {
|
||||
cleanup()
|
||||
}
|
||||
|
||||
if p := recover(); p != nil {
|
||||
logx.ErrorStack(p)
|
||||
}
|
||||
}
|
||||
28
core/rescue/recover_test.go
Normal file
28
core/rescue/recover_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package rescue
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"zero/core/logx"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
logx.Disable()
|
||||
}
|
||||
|
||||
func TestRescue(t *testing.T) {
|
||||
var count int32
|
||||
assert.NotPanics(t, func() {
|
||||
defer Recover(func() {
|
||||
atomic.AddInt32(&count, 2)
|
||||
}, func() {
|
||||
atomic.AddInt32(&count, 3)
|
||||
})
|
||||
|
||||
panic("hello")
|
||||
})
|
||||
assert.Equal(t, int32(5), atomic.LoadInt32(&count))
|
||||
}
|
||||
Reference in New Issue
Block a user