forked from pingcap/errcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap_test.go
157 lines (143 loc) · 4.91 KB
/
wrap_test.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
package errcode_test
import (
"testing"
"github.com/gregwebs/errcode"
"github.com/gregwebs/errors"
)
func TestErrorWrapperNil(t *testing.T) {
// Don't panic!
if errcode.Wrap(errcode.ErrorCode(nil), "wrapped") != nil {
t.Errorf("Wrap nil should be nil")
}
if errcode.Wrap(errcode.ErrorCode(nil), "wrapped") != nil {
t.Errorf("Wrapf nil should be nil")
}
if errcode.Wraps(errcode.ErrorCode(nil), "wrapped") != nil {
t.Errorf("Wraps nil should be nil")
}
}
func TestErrorWrapperFunctions(t *testing.T) {
underlying := errors.New("underlying")
{
bad := errcode.NewBadRequestErr(underlying)
AssertCode(t, bad, errcode.InvalidInputCode.CodeStr())
coded := errcode.Wrap(bad, "wrapped")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "wrapped: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
doubleUnwrap := errors.Unwrap(errors.Unwrap(coded))
if doubleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", doubleUnwrap.Error())
}
}
{
bad := errcode.NewBadRequestErr(underlying)
AssertCode(t, bad, errcode.InvalidInputCode.CodeStr())
coded := errcode.Wrapf(bad, "wrapped %s", "arg")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "wrapped arg: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
doubleUnwrap := errors.Unwrap(errors.Unwrap(coded))
if doubleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", doubleUnwrap.Error())
}
}
{
bad := errcode.NewBadRequestErr(underlying)
AssertCode(t, bad, errcode.InvalidInputCode.CodeStr())
coded := errcode.Wraps(bad, "wrapped", "arg", 1)
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "wrapped arg=1: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
doubleUnwrap := errors.Unwrap(errors.Unwrap(coded))
if doubleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", doubleUnwrap.Error())
}
}
}
func TestUserWrapperFunctions(t *testing.T) {
underlying := errors.New("underlying")
{
coded := errcode.WithUserMsg("user",
errcode.NewBadRequestErr(underlying),
)
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
coded = errcode.WrapUser(coded, "wrapped")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "user: wrapped: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
tripleUnwrap := errors.Unwrap(errors.Unwrap(errors.Unwrap(coded)))
if tripleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", tripleUnwrap.Error())
}
}
{
coded := errcode.WithUserMsg("user",
errcode.NewBadRequestErr(underlying),
)
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
coded = errcode.WrapfUser(coded, "wrapped %s", "arg")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "user: wrapped arg: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
tripleUnwrap := errors.Unwrap(errors.Unwrap(errors.Unwrap(coded)))
if tripleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", tripleUnwrap.Error())
}
}
{
coded := errcode.WithUserMsg("user",
errcode.NewBadRequestErr(underlying),
)
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
coded = errcode.WrapsUser(coded, "wrapped", "arg", 1)
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "user: wrapped arg=1: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
tripleUnwrap := errors.Unwrap(errors.Unwrap(errors.Unwrap(coded)))
if tripleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", tripleUnwrap.Error())
}
}
}
func TestManyWrappings(t *testing.T) {
underlying := errors.New("underlying")
{
user := errcode.WithUserMsg("user",
errcode.NewBadRequestErr(underlying),
)
opCode := errcode.Op("op").AddTo(user)
AssertCode(t, opCode, errcode.InvalidInputCode.CodeStr())
coded := errcode.WrapOp(opCode, "wrapped")
AssertCode(t, coded, errcode.InvalidInputCode.CodeStr())
if errMsg := coded.Error(); errMsg != "op: user: wrapped: underlying" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
tripleUnwrap := errors.Unwrap(errors.Unwrap(errors.Unwrap(errors.Unwrap(coded))))
if tripleUnwrap.Error() != underlying.Error() {
t.Errorf("bad unwrap: %s", tripleUnwrap.Error())
}
}
}
func TestWrapNotInPlace(t *testing.T) {
user := errcode.WithUserMsg("user",
MinimalError{},
)
op := errcode.Op("op").AddTo(user)
AssertCode(t, op, codeString)
coded := errcode.Wrap(op, "wrapped")
AssertCode(t, coded, codeString)
if errMsg := coded.Error(); errMsg != "wrapped: op: user: error" {
t.Errorf("Wrap unexpected: %s", errMsg)
}
tripleUnwrap := errors.Unwrap(errors.Unwrap(errors.Unwrap(errors.Unwrap(coded))))
if tripleUnwrap.Error() != (MinimalError{}).Error() {
t.Errorf("bad unwrap: %s", tripleUnwrap.Error())
}
}