-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_formatter_test.go
230 lines (204 loc) · 5.93 KB
/
json_formatter_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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
package errors
import (
"encoding/json"
se "errors"
"reflect"
"testing"
)
func TestWithStopStackOn(t *testing.T) {
opt := WithStopStackOn("testing")
options := &ErrorFormattingOptions{}
opt(options)
if options.stopStackOn != "testing" {
t.Errorf("expected 'fasthttp', got '%s'", options.stopStackOn)
}
}
func TestWithAttributes(t *testing.T) {
opt := WithAttributes(AddStack | AddFields)
options := &ErrorFormattingOptions{}
opt(options)
if options.include != (AddStack | AddFields) {
t.Errorf("expected '%v', got '%v'", AddStack|AddFields, options.include)
}
}
func TestWithRootLevelFields(t *testing.T) {
fields := []string{"field1", "field2"}
opt := WithRootLevelFields(fields)
options := &ErrorFormattingOptions{}
opt(options)
if !reflect.DeepEqual(options.rootLevelFields, fields) {
t.Errorf("expected '%v', got '%v'", fields, options.rootLevelFields)
}
}
func TestToJSON(t *testing.T) {
err := &Error{
attrs: attrs{
message: "test error",
severity: SeverityLevel(1),
statusCode: 500,
code: "E500",
},
}
options := []Option{
WithAttributes(AddStack | AddFields),
WithRootLevelFields([]string{"field1"}),
}
jsonBytes := ToJSON(err, options...)
var jsonResponse JSONErrorResponse
unmarshalErr := json.Unmarshal(jsonBytes, &jsonResponse)
if unmarshalErr != nil {
t.Errorf("unexpected error: %v", unmarshalErr)
}
if jsonResponse.Message != "test error" {
t.Errorf("expected 'test error', got '%s'", jsonResponse.Message)
}
if jsonResponse.Severity != smedium {
t.Errorf("expected 'medium', got '%s'", jsonResponse.Severity)
}
if jsonResponse.Code != "E500" {
t.Errorf("expected 'E500', got '%s'", jsonResponse.Code)
}
if jsonResponse.StatusCode != 500 {
t.Errorf("expected 500, got %d", jsonResponse.StatusCode)
}
}
func TestDefaultErrorMarshaler(t *testing.T) {
message := "test error"
severity := Critical
statusCode := 500
code := "E500"
jsonBytes := DefaultErrorMarshaler(message, severity, statusCode, code)
var jsonResponse map[string]interface{}
unmarshalErr := json.Unmarshal(jsonBytes, &jsonResponse)
if unmarshalErr != nil {
t.Errorf("unexpected error: %v", unmarshalErr)
}
if jsonResponse["message"] != message {
t.Errorf("expected '%s', got '%s'", message, jsonResponse["message"])
}
if jsonResponse["severity"] != scritical {
t.Errorf("expected 'critical', got '%s'", jsonResponse["severity"])
}
if jsonResponse["statusCode"] != float64(statusCode) {
t.Errorf("expected %d, got %f", statusCode, jsonResponse["statusCode"])
}
if jsonResponse["code"] != code {
t.Errorf("expected '%s', got '%s'", code, jsonResponse["code"])
}
}
func TestWithStopStackOnEmpty(t *testing.T) {
opt := WithStopStackOn("")
options := &ErrorFormattingOptions{}
opt(options)
if options.stopStackOn != "" {
t.Errorf("expected empty string, got '%s'", options.stopStackOn)
}
}
func TestWithAttributesNone(t *testing.T) {
opt := WithAttributes(0)
options := &ErrorFormattingOptions{}
opt(options)
if options.include != 0 {
t.Errorf("expected 0, got '%v'", options.include)
}
}
func TestWithRootLevelFieldsEmpty(t *testing.T) {
fields := []string{}
opt := WithRootLevelFields(fields)
options := &ErrorFormattingOptions{}
opt(options)
if len(options.rootLevelFields) != 0 {
t.Errorf("expected empty slice, got '%v'", options.rootLevelFields)
}
}
func TestToJSONWithContext(t *testing.T) {
embeddedError := New("embedded error").Code("E1234").StatusCode(400)
err := &Error{
attrs: attrs{
message: "test error",
severity: Medium,
statusCode: 500,
code: "E500",
},
err: embeddedError,
stack: DefaultCallerFrames(0),
}
tcases := []struct {
options []Option
fields map[string]interface{}
}{
{
options: []Option{
WithAttributes(AddStack | AddFields | AddWrappedErrors),
WithRootLevelFields([]string{"field1"}),
},
fields: map[string]interface{}{
"field2": "value2",
},
},
{
options: []Option{
WithAttributes(AddStack | AddFields | AddWrappedErrors),
},
fields: map[string]interface{}{
"field1": "value1",
"field2": "value2",
},
},
}
for _, tcase := range tcases {
err.Set("field1", "value1").Set("field2", "value2")
jsonBytes := ToJSON(err, tcase.options...)
var jsonResponse JSONErrorResponse
unmarshalErr := json.Unmarshal(jsonBytes, &jsonResponse)
if unmarshalErr != nil {
t.Errorf("unexpected error: %v", unmarshalErr)
}
if jsonResponse.Message != "test error" {
t.Errorf("expected 'test error', got '%s'", jsonResponse.Message)
}
if jsonResponse.Severity != smedium {
t.Errorf("expected 'medium', got '%s'", jsonResponse.Severity)
}
if jsonResponse.Code != "E500" {
t.Errorf("expected 'E500', got '%s'", jsonResponse.Code)
}
if jsonResponse.StatusCode != 500 {
t.Errorf("expected 500, got %d", jsonResponse.StatusCode)
}
if len(jsonResponse.Fields) != len(tcase.fields) {
t.Errorf("expected %d fields, got %d", len(tcase.fields), len(jsonResponse.Fields))
}
for k, v := range tcase.fields {
if jsonResponse.Fields[k] != v {
t.Errorf("expected '%v', got '%v'", v, jsonResponse.Fields[k])
}
}
if len(jsonResponse.Stack) != 1 {
t.Errorf("expected 1 stack frame, got %d", len(jsonResponse.Stack))
}
if len(jsonResponse.Wrapped) != 1 {
t.Errorf("expected 1 wrapped error, got %d", len(jsonResponse.Wrapped))
}
}
}
func TestToJSON_error(t *testing.T) {
if ToJSON(nil) != nil {
t.Errorf("expected nil, got %v", ToJSON(nil))
}
err := se.New("test error")
expectedResponse := `{"msg": "test error"}`
if response := string(ToJSON(err)); response != expectedResponse {
t.Errorf("expected '%s', got '%s'", expectedResponse, response)
}
}
func TestToJSON_identMarshal(t *testing.T) {
err := New("test error").Raise()
expectedResponse := `{
"msg": "test error",
"severity": "tiny"
}`
if response := string(ToJSON(err, WithAttributes(IndentJSON))); response != expectedResponse {
t.Errorf("expected '%s', got '%s'", expectedResponse, response)
}
}