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:
yangtao
2023-05-08 23:08:31 +08:00
committed by GitHub
parent a93c24ce84
commit 851a72f1cc
3 changed files with 81 additions and 1 deletions

View File

@@ -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())
}
}