add recover

This commit is contained in:
谢小军
2020-09-04 16:15:58 +08:00
parent 20df66db97
commit 8004c9db81
7 changed files with 52 additions and 8 deletions

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"go.inferGopath": false
}

View File

@@ -71,6 +71,11 @@ func (b *_Base) handlerFuncObj(tvl, obj reflect.Value, methodName string) gin.Ha
}
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
b.recoverErrorFunc(err)
}
}()
tvl.Call([]reflect.Value{obj, reflect.ValueOf(apiFun(c))})
}
}
@@ -154,6 +159,12 @@ func (b *_Base) getCallFunc3(tvl reflect.Value) (func(*gin.Context), error) {
}
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
b.recoverErrorFunc(err)
}
}()
req := reflect.New(reqType)
if !reqIsValue {
req = reflect.New(reqType.Elem())
@@ -224,6 +235,12 @@ func (b *_Base) getCallObj3(tvl, obj reflect.Value, methodName string) (func(*gi
}
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
b.recoverErrorFunc(err)
}
}()
req := reflect.New(reqType)
if !reqIsValue {
req = reflect.New(reqType.Elem())

Binary file not shown.

3
def.go
View File

@@ -21,6 +21,9 @@ func _fun3(*api.Context, interface{}) {}
// NewAPIFunc Custom context support
type NewAPIFunc func(*gin.Context) interface{}
// RecoverErrorFunc recover 错误设置
type RecoverErrorFunc func(interface{})
// parmInfo 参数类型描述
type parmInfo struct {
Pkg string // 包名

View File

@@ -5,6 +5,8 @@ import (
"runtime"
"strings"
"github.com/xxjwxc/public/mylog"
"github.com/gin-gonic/gin"
"github.com/xxjwxc/ginrpc/api"
"github.com/xxjwxc/public/errors"
@@ -12,13 +14,14 @@ import (
// _Base base struct
type _Base struct {
isBigCamel bool // big camel style.大驼峰命名规则
isDev bool // if is development
apiFun NewAPIFunc
apiType reflect.Type
outPath string // output path.输出目录
beforeAfter GinBeforeAfter
isOutDoc bool
isBigCamel bool // big camel style.大驼峰命名规则
isDev bool // if is development
apiFun NewAPIFunc
apiType reflect.Type
outPath string // output path.输出目录
beforeAfter GinBeforeAfter
isOutDoc bool
recoverErrorFunc RecoverErrorFunc
}
// Option overrides behavior of Connect.
@@ -82,6 +85,9 @@ func Default() *_Base {
b := new(_Base)
b.Model(api.NewAPIFunc)
b.Dev(true)
b.SetRecover(func(err interface{}) {
mylog.Error(err)
})
return b
}
@@ -94,6 +100,9 @@ func New(opts ...Option) *_Base {
o.apply(b)
}
b.SetRecover(func(err interface{}) {
mylog.Error(err)
})
return b
}
@@ -102,6 +111,11 @@ func (b *_Base) Dev(isDev bool) {
b.isDev = isDev
}
// SetRecover set recover err call
func (b *_Base) SetRecover(errfun func(interface{})) {
b.recoverErrorFunc = errfun
}
// OutDoc set if out doc. 设置是否输出接口文档
func (b *_Base) OutDoc(isOutDoc bool) {
b.isOutDoc = isOutDoc
@@ -189,6 +203,11 @@ func (b *_Base) HandlerFunc(handlerFunc interface{}) gin.HandlerFunc { // 获取
if ctxType == b.apiType {
method := reflect.ValueOf(handlerFunc)
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
b.recoverErrorFunc(err)
}
}()
method.Call([]reflect.Value{reflect.ValueOf(b.apiFun(c))})
}
}

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.12
require (
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.2.0
github.com/xxjwxc/public v0.0.0-20200806170246-c98b0706a117
github.com/xxjwxc/public v0.0.0-20200827144729-d6b92b599629
)
// replace github.com/xxjwxc/public => ../public

2
go.sum
View File

@@ -172,6 +172,8 @@ github.com/xxjwxc/gowp v0.0.0-20200603130651-4d7368b0e285/go.mod h1:yJ/fY5BorWAR
github.com/xxjwxc/public v0.0.0-20200603115833-341beff27850/go.mod h1:fp3M+FEQrCgWD1fZ/PLwZkCTglf086OEhC9LcydAUnc=
github.com/xxjwxc/public v0.0.0-20200806170246-c98b0706a117 h1:b0n+CBXKl6rxu+v6v2kwVhBn2sJgnugzaTJ/FIPY/dw=
github.com/xxjwxc/public v0.0.0-20200806170246-c98b0706a117/go.mod h1:0BFWVHqt7nKW8MtIx7R7bOkoGQFFnKsaJeeVbkzY88E=
github.com/xxjwxc/public v0.0.0-20200827144729-d6b92b599629 h1:aCs7EJxuv/8sHytHzKSWVo6LdkI1qZtkBHJG2j/sCNA=
github.com/xxjwxc/public v0.0.0-20200827144729-d6b92b599629/go.mod h1:0BFWVHqt7nKW8MtIx7R7bOkoGQFFnKsaJeeVbkzY88E=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=