-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
43 lines (39 loc) · 956 Bytes
/
json_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
package serrs_test
import (
"encoding/json"
"errors"
"testing"
"github.com/ryomak/serrs"
)
func TestStackedErrorJson(t *testing.T) {
tests := []struct {
name string
in error
want string
}{
{
name: "simpleError",
in: serrs.Wrap(
serrs.New(serrs.DefaultCode("unexpected"), "unexpected error"),
serrs.WithMessage("wrap error"),
serrs.WithData(serrs.DefaultCustomData{"key1": "value1"}),
),
want: `[{"message":"unexpected error","data":null},{"message":"wrap error","data":{"key1":"value1"}}]`,
},
{
name: "pure error",
in: errors.New("unexpected error"),
want: `[{"message":"unexpected error","data":null}]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := serrs.StackedErrorJson(tt.in)
outJson, err := json.Marshal(out)
if err != nil {
t.Errorf("json.Marshal() = %v, want %v", outJson, tt.want)
}
checkEqual(t, string(outJson), tt.want)
})
}
}