-
Notifications
You must be signed in to change notification settings - Fork 5
/
errors_test.go
111 lines (95 loc) · 2.91 KB
/
errors_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
package don_test
import (
"encoding/xml"
"errors"
"fmt"
"testing"
"github.com/abemedia/go-don"
_ "github.com/abemedia/go-don/encoding/json"
_ "github.com/abemedia/go-don/encoding/text"
_ "github.com/abemedia/go-don/encoding/xml"
_ "github.com/abemedia/go-don/encoding/yaml"
"github.com/goccy/go-json"
"github.com/google/go-cmp/cmp"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v3"
)
func TestError_Is(t *testing.T) {
if !errors.Is(don.Error(errTest, 0), errTest) {
t.Error("should match wrapped error")
}
if !errors.Is(don.Error(errTest, fasthttp.StatusBadRequest), don.ErrBadRequest) {
t.Error("should match status error")
}
}
func TestError_Unwrap(t *testing.T) {
if errors.Unwrap(don.Error(errTest, 0)) != errTest { //nolint:errorlint
t.Error("should unwrap wrapped error")
}
}
func TestError_StatusCode(t *testing.T) {
if don.Error(don.ErrBadRequest, 0).StatusCode() != fasthttp.StatusBadRequest {
t.Error("should respect wrapped error's status code")
}
if don.Error(don.ErrBadRequest, fasthttp.StatusConflict).StatusCode() != fasthttp.StatusConflict {
t.Error("should ignore wrapped error's status code if explicitly set")
}
}
func TestError_MarshalText(t *testing.T) {
b, _ := don.Error(errTest, 0).MarshalText()
if diff := cmp.Diff("test", string(b)); diff != "" {
t.Error(diff)
}
b, _ = don.Error(&testError{}, 0).MarshalText()
if diff := cmp.Diff("custom", string(b)); diff != "" {
t.Error(diff)
}
}
func TestError_MarshalJSON(t *testing.T) {
b, _ := json.Marshal(don.Error(errTest, 0))
if diff := cmp.Diff(`{"message":"test"}`, string(b)); diff != "" {
t.Error(diff)
}
b, _ = json.Marshal(don.Error(&testError{}, 0))
if diff := cmp.Diff(`{"custom":"test"}`, string(b)); diff != "" {
t.Error(diff)
}
}
func TestError_MarshalXML(t *testing.T) {
b, _ := xml.Marshal(don.Error(errTest, 0))
if diff := cmp.Diff("<message>test</message>", string(b)); diff != "" {
t.Error(diff)
}
b, _ = xml.Marshal(don.Error(&testError{}, 0))
if diff := cmp.Diff("<custom>test</custom>", string(b)); diff != "" {
t.Error(diff)
}
}
func TestError_MarshalYAML(t *testing.T) {
b, _ := yaml.Marshal(don.Error(errTest, 0))
if diff := cmp.Diff("message: test\n", string(b)); diff != "" {
t.Error(diff)
}
b, _ = yaml.Marshal(don.Error(&testError{}, 0))
if diff := cmp.Diff("custom: test\n", string(b)); diff != "" {
t.Error(diff)
}
}
var errTest = errors.New("test")
type testError struct{}
func (e *testError) Error() string {
return "test"
}
func (e *testError) MarshalText() ([]byte, error) {
return []byte("custom"), nil
}
func (e *testError) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`{"custom":%q}`, e.Error())), nil
}
func (e *testError) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "custom"}
return enc.EncodeElement(e.Error(), start)
}
func (e *testError) MarshalYAML() (any, error) {
return map[string]string{"custom": e.Error()}, nil
}