From b654ad332fd6796df16454897f4d7cf94b0f3ba1 Mon Sep 17 00:00:00 2001 From: glennliao Date: Sat, 8 Jul 2023 14:11:03 +0800 Subject: [PATCH] ## 0.2.0-beta10 fix some --- action/action.go | 15 ++++++++---- config/action_config.go | 2 +- config/config.go | 2 +- config/functions.go | 4 +-- config/query_config.go | 2 +- config/request_config.go | 3 +-- consts/errors.go | 7 ++++++ drivers/goframe/web/gf.go | 51 +++++++++++++++++++++++++++++---------- query/node_func.go | 5 ++++ 9 files changed, 66 insertions(+), 25 deletions(-) diff --git a/action/action.go b/action/action.go index afa5d69..468f9df 100755 --- a/action/action.go +++ b/action/action.go @@ -48,7 +48,9 @@ func New(ctx context.Context, actionConfig *config.ActionConfig, method string, request, err := checkTag(req, method, actionConfig) if err != nil { - panic(err) + return &Action{ + err: err, + } } delete(req, consts.Tag) @@ -68,6 +70,10 @@ func New(ctx context.Context, actionConfig *config.ActionConfig, method string, func (a *Action) parse() error { + if a.err != nil { + return a.err + } + structures := a.tagRequest.Structure for key, v := range a.req { @@ -138,10 +144,9 @@ func (a *Action) Result() (model.Map, error) { transactionHandler := noTransactionHandler - if *a.tagRequest.Transaction == true { - transactionResolver = GetTransactionHandler - h := transactionResolver(a.ctx, a) - if h != nil { + if a.tagRequest.Transaction != nil && *a.tagRequest.Transaction == true { + h := GetTransactionHandler(a.ctx, a) + if h == nil { err = consts.NewSysErr("transaction handler is nil") return nil, err } diff --git a/config/action_config.go b/config/action_config.go index ad77ab9..82d6c80 100755 --- a/config/action_config.go +++ b/config/action_config.go @@ -26,7 +26,7 @@ func (c *ActionConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig return c.access.GetAccess(key, noVerify) } -func (c *ActionConfig) Func(name string) Func { +func (c *ActionConfig) Func(name string) *Func { return c.functions.funcMap[name] } diff --git a/config/config.go b/config/config.go index cd244c8..3b8f098 100755 --- a/config/config.go +++ b/config/config.go @@ -71,7 +71,7 @@ func New() *Config { a.JsonFieldStyle = CaseCamel a.Functions = &functions{} - a.Functions.funcMap = make(map[string]Func) + a.Functions.funcMap = make(map[string]*Func) return a } diff --git a/config/functions.go b/config/functions.go index fd8d856..794a828 100755 --- a/config/functions.go +++ b/config/functions.go @@ -21,14 +21,14 @@ type Func struct { } type functions struct { - funcMap map[string]Func + funcMap map[string]*Func } func (f *functions) Bind(name string, _func Func) { if _, exists := f.funcMap[name]; exists { panic(fmt.Errorf(" function %s has exists", name)) } - f.funcMap[name] = _func + f.funcMap[name] = &_func } func (f *functions) Call(ctx context.Context, name string, param g.Map) (any, error) { diff --git a/config/query_config.go b/config/query_config.go index 76c7590..1509182 100755 --- a/config/query_config.go +++ b/config/query_config.go @@ -26,7 +26,7 @@ func (c *QueryConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig, return c.access.GetAccess(key, noVerify) } -func (c *QueryConfig) Func(name string) Func { +func (c *QueryConfig) Func(name string) *Func { return c.functions.funcMap[name] } diff --git a/config/request_config.go b/config/request_config.go index 5804c44..378fedb 100755 --- a/config/request_config.go +++ b/config/request_config.go @@ -4,7 +4,6 @@ import ( "strings" "github.com/glennliao/apijson-go/consts" - "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/util/gconv" @@ -84,7 +83,7 @@ func (c *RequestConfigs) GetRequest(tag string, method string, version string) ( request, ok := c.requestMap[key] if !ok { - return nil, gerror.Newf("request[%s]: 404", key) + return nil, consts.NewRequestNoFoundErr(key) } return request, nil diff --git a/consts/errors.go b/consts/errors.go index eda3fc0..b1b9593 100755 --- a/consts/errors.go +++ b/consts/errors.go @@ -57,6 +57,13 @@ func NewAccessNoFoundErr(key string) Err { } } +func NewRequestNoFoundErr(key string) Err { + return Err{ + code: 404, + message: "request no found: " + key, + } +} + func NewSysErr(msg string) Err { return Err{ code: 500, diff --git a/drivers/goframe/web/gf.go b/drivers/goframe/web/gf.go index ec8af50..15ece8e 100755 --- a/drivers/goframe/web/gf.go +++ b/drivers/goframe/web/gf.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strconv" "strings" "time" @@ -107,6 +108,25 @@ func sortMap(ctx context.Context, body []byte, res *gmap.ListMap, ret model.Map) return reqSortMap } +func try(ctx context.Context, try func(ctx context.Context) error) (err error) { + defer func() { + if exception := recover(); exception != nil { + if v, ok := exception.(error); ok && gerror.HasStack(v) { + err = v + } else { + err = gerror.Newf(`%+v`, exception) + } + } + }() + err = try(ctx) + return +} + +type CodeErr interface { + Code() int + Error() string +} + func CommonResponse(handler func(ctx context.Context, req model.Map) (res model.Map, err error), mode Mode, debug bool) func(req *ghttp.Request) { return func(req *ghttp.Request) { metaRes := &gmap.ListMap{} @@ -114,7 +134,7 @@ func CommonResponse(handler func(ctx context.Context, req model.Map) (res model. msg := "success" nodeRes := &gmap.ListMap{} - err := g.Try(req.Context(), func(ctx context.Context) { + err := try(req.Context(), func(ctx context.Context) (err error) { ret, err := handler(ctx, req.GetMap()) @@ -125,28 +145,33 @@ func CommonResponse(handler func(ctx context.Context, req model.Map) (res model. nodeRes.Set(k, v) } } - - if err != nil { - panic(err) - } - + return }) if err != nil { - if e, ok := err.(consts.Err); ok { + if e, ok := err.(CodeErr); ok { code = e.Code() + if strconv.Itoa(e.Code())[0] == '4' { + code = e.Code() + msg = e.Error() + } else { + code = 500 + msg = "系统异常" + } } else { code = 500 + msg = "系统异常" } - msg = err.Error() - - if e, ok := err.(*gerror.Error); ok { - g.Log().Stack(false).Error(req.Context(), err, e.Stack()) - } else { - g.Log().Stack(false).Error(req.Context(), err) + if code >= 500 { + if e, ok := err.(*gerror.Error); ok { + g.Log().Stack(false).Error(req.Context(), err, e.Stack()) + } else { + g.Log().Stack(false).Error(req.Context(), err) + } } + } metaRes.Set("ok", code == 200) diff --git a/query/node_func.go b/query/node_func.go index 56198eb..7989688 100755 --- a/query/node_func.go +++ b/query/node_func.go @@ -34,6 +34,11 @@ func (h *funcNode) result() { _func := queryConfig.Func(functionName) + if _func == nil { + n.err = consts.NewValidReqErr("functions not exists: " + functionName) + return + } + if n.isList && _func.Batch { n.later = true return