-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_test.go
More file actions
217 lines (189 loc) · 5.34 KB
/
decode_test.go
File metadata and controls
217 lines (189 loc) · 5.34 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package errdecode_test
import (
"errors"
"fmt"
"testing"
"github.com/iamrgon/errdecode"
)
const (
codeCatchAll = iota + 1000
codeClientError
codeCustomError
codeWrappedError
)
var errClient1 = errors.New("client error 1")
var errClient2 = errors.New("client error 2")
var errWrappedError = fmt.Errorf("%w", errors.New("wrapped error"))
var errUnclassified = errors.New("unclassified error")
type CustomError string
func (e CustomError) Error() string { return string(e) }
func newCustomError(msg string) error {
err := CustomError(msg)
return &err
}
func newDecoder() *errdecode.Decoder {
rules := []errdecode.Rule{
{
Code: codeClientError,
Message: "error.client",
Errors: []error{errClient1, errClient2},
},
{
Code: codeCustomError,
Message: "error.custom",
Match: errdecode.MatcherFunc(func(err error) bool {
var errCustom *CustomError
if errors.As(err, &errCustom) {
return true
}
return false
}),
},
{
Code: codeWrappedError,
Message: "error.wrapped",
Match: func(err error) bool {
if errors.Is(err, errWrappedError) {
return true
}
return false
},
},
}
return errdecode.New(rules)
}
func TestDecoderTranslate(t *testing.T) {
tests := []struct {
name string
err error
wantCode int
wantMsg string
}{
{"unclassified value is returned", errors.New("unmatched"), 0, "unmatched"},
{"error value match", errClient1, codeClientError, "error.client"},
{"second error value in group is matched", errClient2, codeClientError, "error.client"},
{"wrapped error value match", errWrappedError, codeWrappedError, "error.wrapped"},
{"custom error type match", newCustomError("custom error"), codeCustomError, "error.custom"},
}
dec := newDecoder()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := dec.Translate(tt.err)
if msg := err.Error(); msg != tt.wantMsg {
t.Fatalf("unexpected message: got='%s' wantMsg='%s'", msg, tt.wantMsg)
}
// Only classified errors can be code checked.
if ce, ok := err.(errdecode.ClassifiedError); ok {
if code := ce.Code(); code != tt.wantCode {
t.Fatalf("unexpected code: got=%d want=%d", code, tt.wantCode)
}
}
})
}
}
func TestUnclassifiedHandling(t *testing.T) {
errUnclassified = errors.New("unclassified error")
dec := errdecode.New(nil, errdecode.Encoder(func(err error) (int, string) { return 0, "" }))
err := dec.Translate(errUnclassified)
if err != errUnclassified {
t.Fatalf("expected unclassified error value to be returned")
}
}
func TestConfiguredCatchAll(t *testing.T) {
errUnclassified = errors.New("unclassified error")
dec := errdecode.New([]errdecode.Rule{{
Code: codeCatchAll,
Message: "error.catchall",
Match: func(_ error) bool { return true },
}})
err := dec.Translate(errUnclassified)
ce, ok := err.(errdecode.ClassifiedError)
if !ok {
t.Fatalf("expected catch-all to be classified")
}
if ce.Code() != codeCatchAll {
t.Fatalf("expected configured catch-all code got=%d", ce.Code())
}
if ce.Error() != "error.catchall" {
t.Fatalf("expected configured catch-all message got=%s", ce.Error())
}
}
func TestClassifiedErrorUnwrapping(t *testing.T) {
dec := newDecoder()
err := dec.Translate(errClient1) // pre-configured in setup
ce, ok := err.(errdecode.ClassifiedError)
if !ok {
t.Fatalf("expected error to be classified")
}
if !errors.Is(err, errClient1) {
t.Fatalf("decoded error does not wrap encountered error: got=%v want=%v", ce.Unwrap(), errClient1)
}
}
func TestEncoderOption(t *testing.T) {
const (
codeGeneralError = iota + 1000
codeAppDomainError1
)
set := map[int]string{
codeGeneralError: "An unknown error occurred.",
codeAppDomainError1: "Description for application domain error.",
}
dec := errdecode.New(nil, errdecode.Encoder(func(err error) (int, string) {
classify := func(code int) (int, string) { return code, set[code] }
switch err {
case errClient1:
return classify(codeAppDomainError1)
}
return classify(codeGeneralError) // catch-all
}))
tests := []struct {
name string
err error
want string
}{
{"unclassified is handled with catch-all", errors.New("unclassified"), set[codeGeneralError]},
{"classified returns configured nessage", errClient1, set[codeAppDomainError1]},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := dec.Translate(tt.err)
if msg := err.Error(); msg != tt.want {
t.Fatalf("unexpected message: got='%s' want='%s'", msg, tt.want)
}
})
}
}
func TestMessageTranslatorOption(t *testing.T) {
errExample := errors.New("example error")
dec := errdecode.New(
[]errdecode.Rule{{
Code: codeCustomError,
Message: "error.custom_defined",
Errors: []error{errExample},
}},
errdecode.Message(func(msg string) string {
switch msg {
case "error.custom_defined":
return "Translated custom error."
default:
return msg
}
}),
)
tests := []struct {
name string
err error
want string
}{
{"unclassified returns encountered message", errors.New("unclassified"), "unclassified"},
{"classified returns configured translation", errExample, "Translated custom error."},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := dec.Translate(tt.err)
if msg := err.Error(); msg != tt.want {
t.Fatalf("unexpected message: got='%s' want='%s'", msg, tt.want)
}
})
}
}