-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.go
255 lines (200 loc) · 6.6 KB
/
errors.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package gobayeux
import (
"fmt"
)
const (
// ErrClientNotConnected is returned when the client is not connected
ErrClientNotConnected = sentinel("client not connected to server")
// ErrTooManyMessages is returned when there is more than one handshake message
ErrTooManyMessages = sentinel("more messages than expected in handshake response")
// ErrBadChannel is returned when the handshake response is on the wrong channel
ErrBadChannel = sentinel("handshake responses must come back via the /meta/handshake channel")
// ErrFailedToConnect is a general connection error
ErrFailedToConnect = sentinel("connect request was not successful")
// ErrNoSupportedConnectionTypes is returned when the client and server
// aren't able to agree on a connection type
ErrNoSupportedConnectionTypes = sentinel("no supported connection types provided")
// ErrNoVersion is returned when a version is not provided
ErrNoVersion = sentinel("no version specified")
// ErrMissingClientID is returned when the client id has not been set
ErrMissingClientID = sentinel("missing clientID value")
// ErrMissingConnectionType is returned when the connection type is unset
ErrMissingConnectionType = sentinel("missing connectionType value")
)
type sentinel string
func (s sentinel) Error() string {
return string(s)
}
// ConnectionFailedError is returned whenever Connect is called and it fails
type ConnectionFailedError struct {
Err error
}
func (e ConnectionFailedError) Error() string {
return fmt.Sprintf("connection failed (%s)", e.Err)
}
func (e ConnectionFailedError) Unwrap() error {
return e.Err
}
// HandshakeFailedError is returned whenever the handshake fails
type HandshakeFailedError struct {
Err error
}
func (e HandshakeFailedError) Error() string {
return e.Err.Error()
}
func (e HandshakeFailedError) Unwrap() error {
return e.Err
}
func newHandshakeError(msg string) *HandshakeFailedError {
return &HandshakeFailedError{
fmt.Errorf("handshake was not successful: %s", msg),
}
}
// SubscriptionFailedError is returned for any errors on Subscribe
type SubscriptionFailedError struct {
Channels []Channel
Err error
}
func (e SubscriptionFailedError) Error() string {
return fmt.Sprintf("subscription failed (%s)", e.Err)
}
func (e SubscriptionFailedError) Unwrap() error {
return e.Err
}
// UnsubscribeFailedError is returned for any errors on Unsubscribe
type UnsubscribeFailedError struct {
Channels []Channel
Err error
}
func (e UnsubscribeFailedError) Error() string {
return fmt.Sprintf("subscription failed (%s)", e.Err)
}
func (e UnsubscribeFailedError) Unwrap() error {
return e.Err
}
// ActionFailedError is a general purpose error returned by the BayeuxClient
type ActionFailedError struct {
Action string
ErrorMessage string
}
func (e ActionFailedError) Error() string {
return fmt.Sprintf("unable to %s channels: %s", e.Action, e.ErrorMessage)
}
func newSubscribeError(msg string) *ActionFailedError {
return &ActionFailedError{"subscribe to", msg}
}
func newUnsubscribeError(msg string) *ActionFailedError {
return &ActionFailedError{"unsubscribe from", msg}
}
// DisconnectFailedError is returned when the call to Disconnect fails
type DisconnectFailedError struct {
Err error
}
func (e DisconnectFailedError) Error() string {
msg := "unable to disconnect from Bayeux server"
if e.Err == nil {
return msg
}
return fmt.Sprintf("%s (%s)", msg, e.Err)
}
func (e DisconnectFailedError) Unwrap() error {
return e.Err
}
// AlreadyRegisteredError signifies that the given MessageExtender is already
// registered with the client
type AlreadyRegisteredError struct {
MessageExtender
}
func (e AlreadyRegisteredError) Error() string {
return fmt.Sprintf("extension already registered: %s", e.MessageExtender)
}
// BadResponseError is returned when we get an unexpected HTTP response from the server
type BadResponseError struct {
StatusCode int
Status string
}
func (e BadResponseError) Error() string {
return fmt.Sprintf(
"expected 200 response from bayeux server, got %d with status '%s'",
e.StatusCode,
e.Status,
)
}
// BadConnectionTypeError is returned when we don't know how to handle the
// requested connection type
type BadConnectionTypeError struct {
ConnectionType string
}
func (e BadConnectionTypeError) Error() string {
return fmt.Sprintf("%q is not a valid connection type", e.ConnectionType)
}
// BadConnectionVersionError is returned when we can't support the requested
// version number
type BadConnectionVersionError struct {
Version string
}
func (e BadConnectionVersionError) Error() string {
return fmt.Sprintf("version %q is invalid for Bayeux protocol", e.Version)
}
// InvalidChannelError is the result of a failure to validate a channel name
type InvalidChannelError struct {
Channel
}
func (e InvalidChannelError) Error() string {
return fmt.Sprintf("channel %q appears to not be a valid channel", e.Channel)
}
// EmptySliceError is returned when an empty slice is unexpected
type EmptySliceError string
func (e EmptySliceError) Error() string {
return fmt.Sprintf("no %s provided", string(e))
}
// ErrMessageUnparsable is returned when we fail to parse a message
type ErrMessageUnparsable string
func (e ErrMessageUnparsable) Error() string {
return fmt.Sprintf("error message not parseable: %s", string(e))
}
// BadStateError is returned when the state machine transition is not valid
type BadStateError struct {
CurrentState int32
FromState int32
ToState int32
Message string
}
func (e BadStateError) Error() string {
return fmt.Sprintf("%s, (current: %s, from: %s, to: %s)", e.Message, stateName(e.CurrentState), stateName(e.FromState), stateName(e.ToState))
}
// BadHandshakeError is returned when trying to handshake but not unconnected
type BadHandshakeError struct {
*BadStateError
}
func newBadHanshake(current, from, to int32) *BadHandshakeError {
return &BadHandshakeError{
&BadStateError{
Message: "attempting to handshake but not in unconnected state",
CurrentState: current,
FromState: from,
ToState: to,
},
}
}
// BadConnectionError is returned when trying to connected but not connecting
type BadConnectionError struct {
*BadStateError
}
func newBadConnection(current, from, to int32) *BadConnectionError {
return &BadConnectionError{
&BadStateError{
Message: "invalid state for successful connect response event",
CurrentState: current,
FromState: from,
ToState: to,
},
}
}
// UnknownEventTypeError is returned when the next state is unknown
type UnknownEventTypeError struct {
Event
}
func (e UnknownEventTypeError) Error() string {
return fmt.Sprintf("unknown event type (%q)", e.Event)
}