-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherror_test.go
173 lines (167 loc) · 3.71 KB
/
error_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package kv
import (
"context"
"errors"
"testing"
)
// keyvalserError is an error that implements the keyvalser interface.
// Used for testing only.
type keyvalserError []interface{}
func (e keyvalserError) Error() string {
return List(e).String()
}
func (e keyvalserError) Keyvals() []interface{} {
return e
}
func TestError(t *testing.T) {
tests := []struct {
fn func() (err error, cause error)
want string
}{
{
fn: func() (err error, cause error) {
return NewError("message text"), nil
},
want: "message text",
},
{
fn: func() (error, error) {
err := errors.New("first message")
return Wrap(err, "second message"), err
},
want: "second message: first message",
},
{
fn: func() (error, error) {
return NewError("message text").With("a", 1, "b", 2), nil
},
want: "message text a=1 b=2",
},
{
fn: func() (error, error) {
err := NewError("first message").With("a", 1, "b", 2, "c", 3)
return Wrap(err, "second message").With("a", 1, "b", 2, "d", 4), err
},
want: "second message: first message a=1 b=2 d=4 c=3",
},
{
fn: func() (error, error) {
err1 := keyvalserError{"msg", "first", "a", 1, "b", 2, "c", "3"}
err2 := Wrap(err1, "second").With("a", 1, "b", "2")
return Wrap(err2, "third").With("a", 2), err2
},
want: "third: second: first a=2 a=1 b=2 c=3",
},
{
fn: func() (error, error) {
ctx := From(context.Background()).With("c", 3, "d", 4)
err1 := keyvalserError{"msg", "first", "a", 1, "b", 2, "c", "3"}
err2 := Wrap(err1, "second").With("a", 1, "b", "2")
return From(ctx).Wrap(err2, "third").With("a", 2), err2
},
want: "third: second: first a=2 a=1 b=2 c=3 d=4",
},
{
fn: func() (error, error) {
return From(nil).NewError("text"), nil
},
want: "text",
},
{
fn: func() (error, error) {
list := With("a", 1, "b", 2)
err := list.NewError("text")
return err, nil
},
want: "text a=1 b=2",
},
{
fn: func() (error, error) {
list := With("a", 1, "b", 2)
cause := list.NewError("second")
err := list.Wrap(cause, "first")
return err, cause
},
want: "first: second a=1 b=2",
},
{
fn: func() (error, error) {
return nil, nil
},
want: "",
},
}
for tn, tt := range tests {
err, cause := tt.fn()
var errText string
if err != nil {
errText = err.Error()
}
if got, want := errText, tt.want; got != want {
t.Errorf("%d:\n got=%v\nwant=%v", tn, got, want)
continue
}
var unwrapped error
if unwrap, ok := err.(interface{ Unwrap() error }); ok {
unwrapped = unwrap.Unwrap()
}
if got, want := unwrapped, cause; got != want {
t.Errorf("%d:\n got=%v\nwant=%v", tn, got, want)
continue
}
causer, canCause := err.(interface{ Cause() error })
if cause != nil {
if got, want := canCause, true; got != want {
t.Errorf("%d: got=%v, want=%v", tn, got, want)
continue
}
if got, want := causer.Cause(), cause; got != want {
t.Errorf("%d:\n got=%v\nwant=%v", tn, got, want)
continue
}
} else {
if got, want := canCause, false; got != want {
t.Errorf("%d: got=%v, want=%v", tn, got, want)
continue
}
}
}
}
/*
func TestUnwrap(t *testing.T) {
err1 := errors.New("error 1")
tests := []struct {
fn func() error
want error
}{
{
fn: func() error {
return Wrap(err1)
},
want: err1,
},
{
fn: func() error {
return NewError("error")
},
want: nil,
},
{
fn: func() error {
return nil
},
want: nil,
},
}
for tn, tt := range tests {
err := tt.fn()
var got error
if unwrap, ok := err.(interface{ Unwrap() error }); ok {
got = unwrap.Unwrap()
}
if want := tt.want; got != want {
t.Errorf("%d:\n got=%v\nwant=%v", tn, got, want)
}
}
}
*/