Add RunSafe with context (#3224)
Co-authored-by: yangtao <mrynag8614@163.com> Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
package rescue
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/logx"
|
||||
import (
|
||||
"context"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logc"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// Recover is used with defer to do cleanup on panics.
|
||||
// Use it like:
|
||||
@@ -15,3 +21,13 @@ func Recover(cleanups ...func()) {
|
||||
logx.ErrorStack(p)
|
||||
}
|
||||
}
|
||||
|
||||
func RecoverCtx(ctx context.Context, cleanups ...func()) {
|
||||
for _, cleanup := range cleanups {
|
||||
cleanup()
|
||||
}
|
||||
|
||||
if p := recover(); p != nil {
|
||||
logc.Errorf(ctx, "%+v\n\n%s", p, debug.Stack())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user