-
Notifications
You must be signed in to change notification settings - Fork 0
/
reject.go
56 lines (52 loc) · 1.41 KB
/
reject.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
package xerr
import (
"github.com/stretchr/testify/assert"
"log"
"strconv"
"testing"
)
type IRejecter interface {
Error() string
Resp() Resp
}
// reject 被设计成不公开类型,如果需要在自己的项目选中传递 reject,可以使用 IRejecter 接口传递
type reject struct {
Code int32 `json:"-" xml:"-"`
Message string `json:"-" xml:"-"`
ShouldRecord bool `json:"-" xml:"-"`
}
func (reject reject) Error() string {
return strconv.Itoa(int(reject.Code)) + ":" + reject.Message
}
func (reject reject) Resp() Resp {
return NewResp(reject.Code, reject.Message)
}
func AsReject(err error) (rejectValue *reject, asReject bool) {
asReject = As(err, &rejectValue)
return
}
func Reject(code int32, publicMessage string, shouldRecord bool) error {
if code == 0 {
log.Print("xerr.Reject(code, message, shouldRecord) code can not be zero")
code = 1
}
return WithStack(&reject{
Code: code,
Message: publicMessage,
ShouldRecord: shouldRecord,
})
}
func EqualRejectCode(t *testing.T, err error, code int32) {
reject, asReject := AsReject(err)
assert.True(t, asReject, err.Error(), " not xerr.reject")
if asReject {
assert.Equal(t, reject.Code, code)
}
}
func EqualRejectMessage(t *testing.T, err error, message string) {
reject, asReject := AsReject(err)
assert.True(t, asReject, err.Error(), " not xerr.reject")
if asReject {
assert.Equal(t, reject.Message, message)
}
}