Skip to content

Commit

Permalink
## 0.2.0-beta10 fix some
Browse files Browse the repository at this point in the history
  • Loading branch information
glennliao committed Jul 8, 2023
1 parent decda6d commit b654ad3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
15 changes: 10 additions & 5 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion config/action_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions config/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion config/query_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
3 changes: 1 addition & 2 deletions config/request_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions consts/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
51 changes: 38 additions & 13 deletions drivers/goframe/web/gf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -107,14 +108,33 @@ 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{}
code := 200
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())

Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions query/node_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b654ad3

Please sign in to comment.