-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
208 lines (177 loc) · 5.71 KB
/
error_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
package harpy_test
import (
"encoding/json"
"errors"
. "github.com/dogmatiq/harpy"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("type Error", func() {
Describe("func NewError()", func() {
It("panics if the error code is reserved", func() {
Expect(func() {
NewError(InternalErrorCode)
}).To(PanicWith("the error code -32603 is reserved by the JSON-RPC specification (internal server error)"))
})
})
Describe("func NewErrorWithReservedCode()", func() {
It("panics if the error code is not reserved", func() {
Expect(func() {
NewErrorWithReservedCode(100)
}).To(PanicWith("the error code 100 is not reserved by the JSON-RPC specification"))
})
})
Describe("func NewClientSideError()", func() {
It("does not panic if the error code is reserved", func() {
err := NewClientSideError(
InternalErrorCode,
"<message>",
nil,
)
Expect(err.Code()).To(Equal(InternalErrorCode))
})
})
Describe("func MethodNotFound()", func() {
It("returns an error with the correct error code", func() {
e := MethodNotFound()
Expect(e.Code()).To(Equal(MethodNotFoundCode))
})
})
Describe("func InvalidParameters()", func() {
It("returns an error with the correct error code", func() {
e := InvalidParameters()
Expect(e.Code()).To(Equal(InvalidParametersCode))
})
})
Describe("func Code()", func() {
It("returns the error code", func() {
e := NewError(100)
Expect(e.Code()).To(BeEquivalentTo(100))
})
})
Describe("func Message()", func() {
It("returns the user-defined message", func() {
e := NewError(100, WithMessage("<message>"))
Expect(e.Message()).To(Equal("<message>"))
})
It("returns the error code description when there is no user-defined message", func() {
e := NewError(100)
Expect(e.Message()).To(Equal("unknown error"))
})
When("a causal error is provided", func() {
It("returns the error message of the cause", func() {
cause := errors.New("<cause>")
e := NewError(100, WithCause(cause))
Expect(e.Message()).To(Equal("<cause>"))
})
It("does not override a user-defined message", func() {
cause := errors.New("<cause>")
e := NewError(100, WithMessage("<message>"), WithCause(cause))
Expect(e.Message()).To(Equal("<message>"))
})
})
})
Describe("func MarshalData()", func() {
It("returns the JSON representation of the user-defined data (server side)", func() {
e := NewError(100, WithData("<data>"))
data, ok, err := e.MarshalData()
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(data).To(Equal(json.RawMessage(`"\u003cdata\u003e"`)))
})
It("returns the JSON representation of the user-defined data (client side)", func() {
e := NewClientSideError(
100,
"<message>",
json.RawMessage(`"<data>"`),
)
data, ok, err := e.MarshalData()
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(data).To(Equal(json.RawMessage(`"<data>"`)))
})
It("returns false if there is no user-defined data", func() {
e := NewError(100)
_, ok, err := e.MarshalData()
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeFalse())
})
It("returns an error if the user-defined data cannot be marshaled", func() {
e := NewError(100, WithData(make(chan struct{})))
_, _, err := e.MarshalData()
Expect(err).To(MatchError("json: unsupported type: chan struct {}"))
})
})
Describe("func UnmarshalData()", func() {
It("unmarshals the user-defined data", func() {
e := NewError(100, WithData("<data>"))
var v any
ok, err := e.UnmarshalData(&v)
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(v).To(Equal("<data>"))
})
It("unmarshals the user-defined data (client side)", func() {
e := NewClientSideError(
100,
"<message>",
json.RawMessage(`"<data>"`),
)
var v any
ok, err := e.UnmarshalData(&v)
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(v).To(Equal("<data>"))
})
It("returns false if there is no user-defined data", func() {
e := NewError(100)
ok, err := e.UnmarshalData(nil)
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeFalse())
})
It("returns an error if the user-defined data cannot be unmarshaled", func() {
e := NewError(100, WithData("<data>"))
var v int
_, err := e.UnmarshalData(&v)
Expect(err).To(MatchError("json: cannot unmarshal string into Go value of type int"))
})
It("supports the AllowUnknownFields() option", func() {
type In struct {
Foo int `json:"foo"`
Bar int `json:"bar"`
}
type Out struct {
Foo int `json:"foo"`
}
in := In{1, 2}
out := Out{1}
e := NewError(100, WithData(in))
var v Out
ok, err := e.UnmarshalData(&v, AllowUnknownFields(true))
Expect(err).ShouldNot(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(v).To(Equal(out))
})
})
Describe("func Error()", func() {
It("includes the error code description when there is no user-defined message", func() {
e := NewError(100)
Expect(e.Error()).To(Equal("[100] unknown error"))
})
It("includes both the error code description and the user-defined message when the error code is predefined", func() {
e := NewErrorWithReservedCode(InternalErrorCode, WithMessage("<message>"))
Expect(e.Error()).To(Equal("[-32603] internal server error: <message>"))
})
It("includes only the user-defined message when the error code is not predefined", func() {
e := NewError(100, WithMessage("<message>"))
Expect(e.Error()).To(Equal("[100] <message>"))
})
})
Describe("func Unwrap()", func() {
It("returns the causal error", func() {
cause := errors.New("<cause>")
e := NewError(100, WithCause(cause))
Expect(e.Unwrap()).To(BeIdenticalTo(cause))
})
})
})