Skip to content

Commit b654ad3

Browse files
committed
## 0.2.0-beta10 fix some
1 parent decda6d commit b654ad3

File tree

9 files changed

+66
-25
lines changed

9 files changed

+66
-25
lines changed

action/action.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ func New(ctx context.Context, actionConfig *config.ActionConfig, method string,
4848

4949
request, err := checkTag(req, method, actionConfig)
5050
if err != nil {
51-
panic(err)
51+
return &Action{
52+
err: err,
53+
}
5254
}
5355

5456
delete(req, consts.Tag)
@@ -68,6 +70,10 @@ func New(ctx context.Context, actionConfig *config.ActionConfig, method string,
6870

6971
func (a *Action) parse() error {
7072

73+
if a.err != nil {
74+
return a.err
75+
}
76+
7177
structures := a.tagRequest.Structure
7278

7379
for key, v := range a.req {
@@ -138,10 +144,9 @@ func (a *Action) Result() (model.Map, error) {
138144

139145
transactionHandler := noTransactionHandler
140146

141-
if *a.tagRequest.Transaction == true {
142-
transactionResolver = GetTransactionHandler
143-
h := transactionResolver(a.ctx, a)
144-
if h != nil {
147+
if a.tagRequest.Transaction != nil && *a.tagRequest.Transaction == true {
148+
h := GetTransactionHandler(a.ctx, a)
149+
if h == nil {
145150
err = consts.NewSysErr("transaction handler is nil")
146151
return nil, err
147152
}

config/action_config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *ActionConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig
2626
return c.access.GetAccess(key, noVerify)
2727
}
2828

29-
func (c *ActionConfig) Func(name string) Func {
29+
func (c *ActionConfig) Func(name string) *Func {
3030
return c.functions.funcMap[name]
3131
}
3232

config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func New() *Config {
7171
a.JsonFieldStyle = CaseCamel
7272

7373
a.Functions = &functions{}
74-
a.Functions.funcMap = make(map[string]Func)
74+
a.Functions.funcMap = make(map[string]*Func)
7575

7676
return a
7777
}

config/functions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ type Func struct {
2121
}
2222

2323
type functions struct {
24-
funcMap map[string]Func
24+
funcMap map[string]*Func
2525
}
2626

2727
func (f *functions) Bind(name string, _func Func) {
2828
if _, exists := f.funcMap[name]; exists {
2929
panic(fmt.Errorf(" function %s has exists", name))
3030
}
31-
f.funcMap[name] = _func
31+
f.funcMap[name] = &_func
3232
}
3333

3434
func (f *functions) Call(ctx context.Context, name string, param g.Map) (any, error) {

config/query_config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *QueryConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig,
2626
return c.access.GetAccess(key, noVerify)
2727
}
2828

29-
func (c *QueryConfig) Func(name string) Func {
29+
func (c *QueryConfig) Func(name string) *Func {
3030
return c.functions.funcMap[name]
3131
}
3232

config/request_config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"strings"
55

66
"github.com/glennliao/apijson-go/consts"
7-
"github.com/gogf/gf/v2/errors/gerror"
87
"github.com/gogf/gf/v2/frame/g"
98
"github.com/gogf/gf/v2/os/gtime"
109
"github.com/gogf/gf/v2/util/gconv"
@@ -84,7 +83,7 @@ func (c *RequestConfigs) GetRequest(tag string, method string, version string) (
8483
request, ok := c.requestMap[key]
8584

8685
if !ok {
87-
return nil, gerror.Newf("request[%s]: 404", key)
86+
return nil, consts.NewRequestNoFoundErr(key)
8887
}
8988

9089
return request, nil

consts/errors.go

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ func NewAccessNoFoundErr(key string) Err {
5757
}
5858
}
5959

60+
func NewRequestNoFoundErr(key string) Err {
61+
return Err{
62+
code: 404,
63+
message: "request no found: " + key,
64+
}
65+
}
66+
6067
func NewSysErr(msg string) Err {
6168
return Err{
6269
code: 500,

drivers/goframe/web/gf.go

+38-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"strconv"
78
"strings"
89
"time"
910

@@ -107,14 +108,33 @@ func sortMap(ctx context.Context, body []byte, res *gmap.ListMap, ret model.Map)
107108
return reqSortMap
108109
}
109110

111+
func try(ctx context.Context, try func(ctx context.Context) error) (err error) {
112+
defer func() {
113+
if exception := recover(); exception != nil {
114+
if v, ok := exception.(error); ok && gerror.HasStack(v) {
115+
err = v
116+
} else {
117+
err = gerror.Newf(`%+v`, exception)
118+
}
119+
}
120+
}()
121+
err = try(ctx)
122+
return
123+
}
124+
125+
type CodeErr interface {
126+
Code() int
127+
Error() string
128+
}
129+
110130
func CommonResponse(handler func(ctx context.Context, req model.Map) (res model.Map, err error), mode Mode, debug bool) func(req *ghttp.Request) {
111131
return func(req *ghttp.Request) {
112132
metaRes := &gmap.ListMap{}
113133
code := 200
114134
msg := "success"
115135
nodeRes := &gmap.ListMap{}
116136

117-
err := g.Try(req.Context(), func(ctx context.Context) {
137+
err := try(req.Context(), func(ctx context.Context) (err error) {
118138

119139
ret, err := handler(ctx, req.GetMap())
120140

@@ -125,28 +145,33 @@ func CommonResponse(handler func(ctx context.Context, req model.Map) (res model.
125145
nodeRes.Set(k, v)
126146
}
127147
}
128-
129-
if err != nil {
130-
panic(err)
131-
}
132-
148+
return
133149
})
134150

135151
if err != nil {
136152

137-
if e, ok := err.(consts.Err); ok {
153+
if e, ok := err.(CodeErr); ok {
138154
code = e.Code()
155+
if strconv.Itoa(e.Code())[0] == '4' {
156+
code = e.Code()
157+
msg = e.Error()
158+
} else {
159+
code = 500
160+
msg = "系统异常"
161+
}
139162
} else {
140163
code = 500
164+
msg = "系统异常"
141165
}
142166

143-
msg = err.Error()
144-
145-
if e, ok := err.(*gerror.Error); ok {
146-
g.Log().Stack(false).Error(req.Context(), err, e.Stack())
147-
} else {
148-
g.Log().Stack(false).Error(req.Context(), err)
167+
if code >= 500 {
168+
if e, ok := err.(*gerror.Error); ok {
169+
g.Log().Stack(false).Error(req.Context(), err, e.Stack())
170+
} else {
171+
g.Log().Stack(false).Error(req.Context(), err)
172+
}
149173
}
174+
150175
}
151176

152177
metaRes.Set("ok", code == 200)

query/node_func.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ func (h *funcNode) result() {
3434

3535
_func := queryConfig.Func(functionName)
3636

37+
if _func == nil {
38+
n.err = consts.NewValidReqErr("functions not exists: " + functionName)
39+
return
40+
}
41+
3742
if n.isList && _func.Batch {
3843
n.later = true
3944
return

0 commit comments

Comments
 (0)