-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
68 lines (56 loc) · 1.26 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package lrpc
import (
"errors"
"github.com/lazygophers/log"
"github.com/lazygophers/lrpc/middleware/core"
"github.com/lazygophers/lrpc/middleware/xerror"
"reflect"
)
type ListenData struct {
Host string
Port string
TLS bool
}
type Config struct {
Name string
OnError func(ctx *Ctx, err error)
// 用于统一的封包、权限等处理
AfterHandlerFuncWithRef func(ctx *Ctx, data reflect.Value, err error)
AfterHandlerFunc func(ctx *Ctx, err error)
}
var defaultOnError = func(ctx *Ctx, err error) {
var x *xerror.Error
var ok bool
if ok = errors.As(err, &x); !ok {
x = &xerror.Error{
Code: -1,
Msg: err.Error(),
}
}
err = ctx.SendJson(&core.BaseResponse{
Code: x.Code,
Message: x.Msg,
Hint: log.GetTrace(),
})
if err != nil {
log.Errorf("err:%v", err)
return
}
}
var defaultAfterHandlerFuncWithDef = func(ctx *Ctx, data reflect.Value, err error) {
}
var defaultConfig = &Config{
OnError: defaultOnError,
AfterHandlerFuncWithRef: defaultAfterHandlerFuncWithDef,
}
func (p *App) initConfig() {
if p.c == nil {
p.c = defaultConfig
}
if p.c.OnError == nil {
p.c.OnError = defaultOnError
}
if p.c.AfterHandlerFuncWithRef == nil {
p.c.AfterHandlerFuncWithRef = defaultAfterHandlerFuncWithDef
}
}