forked from uber/tchannel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
236 lines (196 loc) · 7.79 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
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"fmt"
"golang.org/x/net/context"
)
const (
// Message id for protocol level errors
invalidMessageID uint32 = 0xFFFFFFFF
)
// A SystemErrCode indicates how a caller should handle a system error returned from a peer
type SystemErrCode byte
//go:generate stringer -type=SystemErrCode
const (
// ErrCodeInvalid is an invalid error code, and should not be used
ErrCodeInvalid SystemErrCode = 0x00
// ErrCodeTimeout indicates the peer timed out. Callers can retry the request
// on another peer if the request is safe to retry.
ErrCodeTimeout SystemErrCode = 0x01
// ErrCodeCancelled indicates that the request was cancelled on the peer. Callers
// can retry the request on the same or another peer if the request is safe to retry
ErrCodeCancelled SystemErrCode = 0x02
// ErrCodeBusy indicates that the request was not dispatched because the peer
// was too busy to handle it. Callers can retry the request on another peer, and should
// reweight their connections to direct less traffic to this peer until it recovers.
ErrCodeBusy SystemErrCode = 0x03
// ErrCodeDeclined indicates that the request not dispatched because the peer
// declined to handle it, typically because the peer is not yet ready to handle it.
// Callers can retry the request on another peer, but should not reweight their connections
// and should continue to send traffic to this peer.
ErrCodeDeclined SystemErrCode = 0x04
// ErrCodeUnexpected indicates that the request failed for an unexpected reason, typically
// a crash or other unexpected handling. The request may have been processed before the failure;
// callers should retry the request on this or another peer only if the request is safe to retry
ErrCodeUnexpected SystemErrCode = 0x05
// ErrCodeBadRequest indicates that the request was malformed, and could not be processed.
// Callers should not bother to retry the request, as there is no chance it will be handled.
ErrCodeBadRequest SystemErrCode = 0x06
// ErrCodeNetwork indicates a network level error, such as a connection reset.
// Callers can retry the request if the request is safe to retry
ErrCodeNetwork SystemErrCode = 0x07
// ErrCodeProtocol indincates a fatal protocol error communicating with the peer. The connection
// will be terminated.
ErrCodeProtocol SystemErrCode = 0xFF
)
var (
// ErrServerBusy is a SystemError indicating the server is busy
ErrServerBusy = NewSystemError(ErrCodeBusy, "server busy")
// ErrRequestCancelled is a SystemError indicating the request has been cancelled on the peer
ErrRequestCancelled = NewSystemError(ErrCodeCancelled, "request cancelled")
// ErrTimeout is a SytemError indicating the request has timed out
ErrTimeout = NewSystemError(ErrCodeTimeout, "timeout")
// ErrTimeoutRequired is a SystemError indicating that timeouts must be specified.
ErrTimeoutRequired = NewSystemError(ErrCodeBadRequest, "timeout required")
// ErrChannelClosed is a SystemError indicating that the channel has been closed.
ErrChannelClosed = NewSystemError(ErrCodeDeclined, "closed channel")
// ErrMethodTooLarge is a SystemError indicating that the method is too large.
ErrMethodTooLarge = NewSystemError(ErrCodeProtocol, "method too large")
)
// MetricsKey is a string representation of the error code that's suitable for
// inclusion in metrics tags.
func (c SystemErrCode) MetricsKey() string {
switch c {
case ErrCodeInvalid:
// Shouldn't ever need this.
return "invalid"
case ErrCodeTimeout:
return "timeout"
case ErrCodeCancelled:
return "cancelled"
case ErrCodeBusy:
return "busy"
case ErrCodeDeclined:
return "declined"
case ErrCodeUnexpected:
return "unexpected-error"
case ErrCodeBadRequest:
return "bad-request"
case ErrCodeNetwork:
return "network-error"
case ErrCodeProtocol:
return "protocol-error"
default:
return c.String()
}
}
func (c SystemErrCode) relayMetricsKey() string {
switch c {
case ErrCodeInvalid:
return "relay-invalid"
case ErrCodeTimeout:
return "relay-timeout"
case ErrCodeCancelled:
return "relay-cancelled"
case ErrCodeBusy:
return "relay-busy"
case ErrCodeDeclined:
return "relay-declined"
case ErrCodeUnexpected:
return "relay-unexpected-error"
case ErrCodeBadRequest:
return "relay-bad-request"
case ErrCodeNetwork:
return "relay-network-error"
case ErrCodeProtocol:
return "relay-protocol-error"
default:
return "relay-" + c.String()
}
}
// A SystemError is a system-level error, containing an error code and message
// TODO(mmihic): Probably we want to hide this interface, and let application code
// just deal with standard raw errors.
type SystemError struct {
code SystemErrCode
msg string
wrapped error
}
// NewSystemError defines a new SystemError with a code and message
func NewSystemError(code SystemErrCode, msg string, args ...interface{}) error {
return SystemError{code: code, msg: fmt.Sprintf(msg, args...)}
}
// NewWrappedSystemError defines a new SystemError wrapping an existing error
func NewWrappedSystemError(code SystemErrCode, wrapped error) error {
if se, ok := wrapped.(SystemError); ok {
return se
}
return SystemError{code: code, msg: fmt.Sprint(wrapped), wrapped: wrapped}
}
// Error returns the code and message, conforming to the error interface
func (se SystemError) Error() string {
return fmt.Sprintf("tchannel error %v: %s", se.Code(), se.msg)
}
// Wrapped returns the wrapped error
func (se SystemError) Wrapped() error { return se.wrapped }
// Code returns the SystemError code, for sending to a peer
func (se SystemError) Code() SystemErrCode {
return se.code
}
// Message returns the SystemError message.
func (se SystemError) Message() string {
return se.msg
}
// GetContextError converts the context error to a tchannel error.
func GetContextError(err error) error {
if err == context.DeadlineExceeded {
return ErrTimeout
}
if err == context.Canceled {
return ErrRequestCancelled
}
return err
}
// GetSystemErrorCode returns the code to report for the given error. If the error is a
// SystemError, we can get the code directly. Otherwise treat it as an unexpected error
func GetSystemErrorCode(err error) SystemErrCode {
if err == nil {
return ErrCodeInvalid
}
if se, ok := err.(SystemError); ok {
return se.Code()
}
return ErrCodeUnexpected
}
// GetSystemErrorMessage returns the message to report for the given error. If the error is a
// SystemError, we can get the underlying message. Otherwise, use the Error() method.
func GetSystemErrorMessage(err error) string {
if se, ok := err.(SystemError); ok {
return se.Message()
}
return err.Error()
}
type errConnNotActive struct {
info string
state connectionState
}
func (e errConnNotActive) Error() string {
return fmt.Sprintf("%v connection is not active: %v", e.info, e.state)
}