diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a460645 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "go.inferGopath": false +} \ No newline at end of file diff --git a/common.go b/common.go index 07611a7..4bea9ed 100644 --- a/common.go +++ b/common.go @@ -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()) diff --git a/conf/gen_router.data b/conf/gen_router.data index 9f2cd6c..2bb3f67 100644 Binary files a/conf/gen_router.data and b/conf/gen_router.data differ diff --git a/def.go b/def.go index b656b50..c5981e7 100644 --- a/def.go +++ b/def.go @@ -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 // 包名 diff --git a/ginrpc.go b/ginrpc.go index 56c7d9f..d26fd58 100644 --- a/ginrpc.go +++ b/ginrpc.go @@ -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))}) } } diff --git a/go.mod b/go.mod index d8f2439..27e6951 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d43a7a5..306d3cf 100644 --- a/go.sum +++ b/go.sum @@ -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=