-
Notifications
You must be signed in to change notification settings - Fork 14
/
error_details_test.go
89 lines (73 loc) · 1.9 KB
/
error_details_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
package errors
import (
"reflect"
"testing"
)
func TestWithDetails(t *testing.T) {
origErr := NewPlain("something went wrong")
details := []interface{}{"key", "value"}
err := WithDetails(origErr, details...)
t.Parallel()
t.Run("error_message", func(t *testing.T) {
checkErrorMessage(t, err, "something went wrong")
})
t.Run("unwrap", func(t *testing.T) {
checkUnwrap(t, err, origErr)
})
t.Run("format", func(t *testing.T) {
checkFormat(t, err, map[string][]string{
"%s": {"something went wrong"},
"%q": {`"something went wrong"`},
"%v": {"something went wrong"},
"%+v": {"something went wrong"},
})
})
t.Run("nil", func(t *testing.T) {
checkErrorNil(t, WithDetails(nil, "key", "value"))
})
t.Run("details", func(t *testing.T) {
d := err.(*withDetails).Details()
for i, detail := range d {
if got, want := detail, details[i]; got != want {
t.Errorf("error detail does not match the expected one\nactual: %+v\nexpected: %+v", got, want)
}
}
})
t.Run("details_missing_value", func(t *testing.T) {
details := []interface{}{"key", nil}
err := WithDetails(origErr, "key")
d := err.(*withDetails).Details()
for i, detail := range d {
if got, want := detail, details[i]; got != want {
t.Errorf("error detail does not match the expected one\nactual: %+v\nexpected: %+v", got, want)
}
}
})
}
func TestGetDetails(t *testing.T) {
err := WithDetails(
WithMessage(
WithDetails(
Wrap(
WithDetails(
New("error"),
"key", "value",
),
"wrapped error",
),
"key2", "value2",
),
"another wrapped error",
),
"key3", "value3",
)
expected := []interface{}{
"key", "value",
"key2", "value2",
"key3", "value3",
}
actual := GetDetails(err)
if got, want := actual, expected; !reflect.DeepEqual(got, want) {
t.Errorf("context does not match the expected one\nactual: %v\nexpected: %v", got, want)
}
}