-
Notifications
You must be signed in to change notification settings - Fork 15
/
options.go
186 lines (147 loc) · 4.04 KB
/
options.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
// Package rkmidpanic provide options
package rkmidpanic
import (
"fmt"
"github.com/rookie-ninja/rk-entry/v2/error"
"github.com/rookie-ninja/rk-query"
"go.uber.org/zap"
"runtime/debug"
)
// ***************** OptionSet Interface *****************
// OptionSetInterface mainly for testing purpose
type OptionSetInterface interface {
GetEntryName() string
GetEntryType() string
Before(*BeforeCtx)
BeforeCtx(event rkquery.Event, logger *zap.Logger, handler handlerFunc) *BeforeCtx
}
// ***************** OptionSet Implementation *****************
// optionSet which is used for middleware implementation
type optionSet struct {
entryName string
entryType string
mock OptionSetInterface
}
// NewOptionSet Create new optionSet with options.
func NewOptionSet(opts ...Option) OptionSetInterface {
set := &optionSet{
entryName: "fake-entry",
entryType: "",
}
for i := range opts {
opts[i](set)
}
if set.mock != nil {
return set.mock
}
return set
}
// GetEntryName returns entry name
func (set *optionSet) GetEntryName() string {
return set.entryName
}
// GetEntryType returns entry type
func (set *optionSet) GetEntryType() string {
return set.entryType
}
// BeforeCtx should be created before Before()
func (set *optionSet) BeforeCtx(event rkquery.Event, logger *zap.Logger, handler handlerFunc) *BeforeCtx {
ctx := NewBeforeCtx()
ctx.Input.Event = event
ctx.Input.Logger = logger
ctx.Input.PanicHandler = handler
return ctx
}
// Before should run before user handler
func (set *optionSet) Before(ctx *BeforeCtx) {
if ctx == nil {
return
}
ctx.Output.DeferFunc = func() {
if recv := recover(); recv != nil {
var res *rkerror.ErrorResp
if se, ok := recv.(*rkerror.ErrorResp); ok {
res = se
} else if re, ok := recv.(error); ok {
res = rkerror.FromError(re)
} else {
res = rkerror.NewInternalError(fmt.Sprintf("%v", recv))
}
if ctx.Input.Event != nil {
ctx.Input.Event.SetCounter("panic", 1)
ctx.Input.Event.AddErr(res.Err)
}
if ctx.Input.Logger != nil {
ctx.Input.Logger.Error(fmt.Sprintf("panic occurs:\n%s", string(debug.Stack())), zap.Error(res.Err))
}
if ctx.Input.PanicHandler != nil {
ctx.Input.PanicHandler(res)
}
}
}
}
// ***************** OptionSet Mock *****************
// NewOptionSetMock for testing purpose
func NewOptionSetMock(before *BeforeCtx) OptionSetInterface {
return &optionSetMock{
before: before,
}
}
type optionSetMock struct {
before *BeforeCtx
}
// GetEntryName returns entry name
func (mock *optionSetMock) GetEntryName() string {
return "mock"
}
// GetEntryType returns entry type
func (mock *optionSetMock) GetEntryType() string {
return "mock"
}
// BeforeCtx should be created before Before()
func (mock *optionSetMock) BeforeCtx(event rkquery.Event, logger *zap.Logger, handler handlerFunc) *BeforeCtx {
return mock.before
}
// Before should run before user handler
func (mock *optionSetMock) Before(ctx *BeforeCtx) {
return
}
// ***************** Context *****************
// NewBeforeCtx create new BeforeCtx with fields initialized
func NewBeforeCtx() *BeforeCtx {
ctx := &BeforeCtx{}
return ctx
}
// BeforeCtx context for Before() function
type BeforeCtx struct {
Input struct {
Event rkquery.Event
Logger *zap.Logger
PanicHandler func(resp *rkerror.ErrorResp)
}
Output struct {
DeferFunc func()
}
}
// ***************** Option *****************
// Option is for middleware while creating
type Option func(*optionSet)
// WithEntryNameAndType Provide entry name and entry type.
func WithEntryNameAndType(entryName, entryType string) Option {
return func(opt *optionSet) {
opt.entryName = entryName
opt.entryType = entryType
}
}
// WithMockOptionSet provide mock OptionSetInterface
func WithMockOptionSet(mock OptionSetInterface) Option {
return func(set *optionSet) {
set.mock = mock
}
}
// User provided handler fun
type handlerFunc func(resp *rkerror.ErrorResp)