-
Notifications
You must be signed in to change notification settings - Fork 2k
/
errors_test.go
139 lines (124 loc) · 3.31 KB
/
errors_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
package errors
import (
stderrors "errors"
"fmt"
"regexp"
"strconv"
"testing"
"github.com/thanos-io/thanos/pkg/testutil"
)
const msg = "test_error_message"
const wrapper = "test_wrapper"
func TestNew(t *testing.T) {
err := New(msg)
testutil.Equals(t, err.Error(), msg, "the root error message must match")
reg := regexp.MustCompile(msg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNew .*\/thanos\/pkg\/errors\/errors_test\.go:17`)
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match")
}
func TestNewFormatted(t *testing.T) {
fmtMsg := msg + " key=%v"
expectedMsg := msg + " key=value"
err := New(fmtMsg, "value")
testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match")
reg := regexp.MustCompile(expectedMsg + `[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestNewFormatted .*\/thanos\/pkg\/errors\/errors_test\.go:28`)
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match")
}
func TestWrap(t *testing.T) {
err := New(msg)
err = Wrap(err, wrapper)
expectedMsg := wrapper + ": " + msg
testutil.Equals(t, err.Error(), expectedMsg, "the root error message must match")
reg := regexp.MustCompile(`test_wrapper[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/thanos\/pkg\/errors\/errors_test\.go:36
[[:ascii:]]+test_error_message[ \n]+> github\.com\/thanos-io\/thanos\/pkg\/errors\.TestWrap .*\/thanos\/pkg\/errors\/errors_test\.go:35`)
testutil.Equals(t, reg.MatchString(fmt.Sprintf("%+v", err)), true, "the stack trace must match")
}
func TestUnwrap(t *testing.T) {
// test with base error
err := New(msg)
for i, tc := range []struct {
err error
expected string
isNil bool
}{
{
// no wrapping
err: err,
isNil: true,
},
{
err: Wrap(err, wrapper),
expected: "test_error_message",
},
{
err: Wrap(Wrap(err, wrapper), wrapper),
expected: "test_wrapper: test_error_message",
},
// check primitives errors
{
err: stderrors.New("std-error"),
isNil: true,
},
{
err: Wrap(stderrors.New("std-error"), wrapper),
expected: "std-error",
},
{
err: nil,
isNil: true,
},
} {
t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) {
unwrapped := Unwrap(tc.err)
if tc.isNil {
testutil.Equals(t, unwrapped, nil)
return
}
testutil.Equals(t, unwrapped.Error(), tc.expected, "Unwrap must match expected output")
})
}
}
func TestCause(t *testing.T) {
// test with base error that implements interface containing Unwrap method
err := New(msg)
for i, tc := range []struct {
err error
expected string
isNil bool
}{
{
// no wrapping
err: err,
isNil: true,
},
{
err: Wrap(err, wrapper),
isNil: true,
},
{
err: Wrap(Wrap(err, wrapper), wrapper),
isNil: true,
},
// check primitives errors
{
err: stderrors.New("std-error"),
expected: "std-error",
},
{
err: Wrap(stderrors.New("std-error"), wrapper),
expected: "std-error",
},
{
err: nil,
isNil: true,
},
} {
t.Run("TestCase"+strconv.Itoa(i), func(t *testing.T) {
cause := Cause(tc.err)
if tc.isNil {
testutil.Equals(t, cause, nil)
return
}
testutil.Equals(t, cause.Error(), tc.expected, "Cause must match expected output")
})
}
}