-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
122 lines (102 loc) · 2.68 KB
/
errors_test.go
File metadata and controls
122 lines (102 loc) · 2.68 KB
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
package errors
import (
"context"
"database/sql"
"errors"
"fmt"
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrors(t *testing.T) {
t.Run("annotate nil", func(t *testing.T) {
err := Annotate(nil, NotFound)
assert.NoError(t, err)
})
code := NotFound
cause := fmt.Errorf("wrapper: %w", sql.ErrNoRows)
err := Annotate(cause, Message(msg), code, resourceInfo)
a, ok := err.(*annotated)
if !assert.True(t, ok) {
return
}
t.Run("annotate", func(t *testing.T) {
messageEx := fmt.Sprintf("%s: %s", msg, cause)
assert.Equal(t, NotFound, a.code)
assert.Equal(t, cause, a.cause)
assert.Equal(t, messageEx, a.message)
})
t.Run("code", func(t *testing.T) {
assert.Equal(t, code, Code(err))
assert.Equal(t, OK, Code(nil))
assert.Equal(t, Unknown, Code(errors.New("unknown error")))
})
t.Run("code2", func(t *testing.T) {
_err := Annotate(NotFound, Message("user"))
assert.Equal(t, NotFound, Code(_err))
})
t.Run("code3", func(t *testing.T) {
assert.Equal(t, Cancelled, Code(context.Canceled))
assert.Equal(t, DeadlineExceeded, Code(context.DeadlineExceeded))
})
t.Run("detail", func(t *testing.T) {
assert.Equal(t, []Any{resourceInfo}, Details(err))
})
t.Run("format", func(t *testing.T) {
strs := []string{
fmt.Sprintf("%v", err),
fmt.Sprintf("%+v", err),
fmt.Sprintf("%q", err),
}
for _, str := range strs {
assert.NotEmpty(t, str)
assert.NotContains(t, str, "!")
}
})
}
func TestTemporary(t *testing.T) {
items := map[error]bool{
OK: false,
Internal: false,
fmt.Errorf("wrap: %w", Internal): false,
Unavailable: true,
fmt.Errorf("wrap: %w", Unavailable): true,
Annotate(&net.DNSError{IsTemporary: true}, Internal): true,
}
for err, ok := range items {
assert.Equal(t, ok, Temporary(err), err.Error())
}
}
func TestFlatten(t *testing.T) {
causes := map[StatusCode]error{
Cancelled: context.Canceled,
DeadlineExceeded: context.DeadlineExceeded,
Unknown: errors.New("unknown error"),
OK: nil,
}
hide := func(typeUrl string) DetailMapper {
return func(a Any) Any {
if typeUrl != a.TypeUrl() {
return a
}
return nil
}
}
mappers := detailMappers{
hide(TypeUrlDebugInfo),
hide(TypeUrlResourceInfo),
}
for code, cause := range causes {
err := Annotate(cause, Message(msg), debugInfo, resourceInfo, errInfo)
err = Flatten(err, mappers...)
a, ok := err.(*annotated)
if !assert.True(t, ok) {
return
}
assert.Equal(t, code, a.code)
for _, detail := range details {
assert.NotEqual(t, debugInfo, detail)
assert.NotEqual(t, resourceInfo, detail)
}
}
}